Part Two: Here is the original picture of the finished product:
This is the second part of a 2-part series on 'How to build a High-Definition FPV UAV using a Raspberry PI with HD camera, using a high speed WiFi link.
In my first post on the subject (located here), I discussed the parts I used, and how to install them into a Hobby King Go-Discover FPV model.
In this post, I will discuss installing the Raspberry PI and the PI camera in the Go-Discover gimbals, and the software configuration for both the Raspberry PI and the ground station PC.
From the previous post, step 3 was completed by installing the Ubiquity Rocket M5 in the model. Now onto step 4:
Step 4: Install the Raspberry PI and PI Camera
Here is a photo of the position of the PI in the Go-Discover model:
The PI fits nicely just behind the camera gimbals, with the USB and HDMI ports on top. In the right side you can see the Cat5 network cable attached. This cable connects to the ethernet switch, which is also connected to the Rocket M5 input port.
The two cables shown on top are the servo control wires for the gimbals, which I have directly connected to channel 4 and 5 on my radio. I am using channel 4 (normally the rudder stick on my radio. Since there is no rudder on a flying wing, this is a convenient channel to use to move left and right with the camera. I have not (yet) moved to a head tracker, but if you already have that setup, just assign the channels accordingly.
To install the PI camera, remove the stock plate from the gimbals (for a GoPro), and mount the PI camera as shown in this photo:
The PI camera case fits very nicely into the slot, and again I used a small piece of velcro to hold it down. You could use a couple of small screws instead if you want a more secure hold. The two gimbals servos are also shown here. They are simple to install, just follow the Go-Discover instructions.
Here is a front view of the PI camera installed:
Here is the block diagram describing all the connections:
Some comments on my previous post suggested that it is possible to eliminate the ethernet switch and serial-to-ethernet converter using the Raspberry PI and a serial port on the PI. I believe this post describes how to talk to the PI via the NavLink, but in this case, I want to use the PI to bridge the connection from the ground station to the APM/PixHawk. Somebody please comment on this if you know more about it. I believe it would require a TCP/IP to serial link from the PI to the telemetry port on the APM, and some software on the PI to act as the bridge. The main connection to the ground station is via the Rocket M5 and TCP/IP, not through a telemetry link (900 Mhz or Zigbee like I used on my other models).
Step 5: Getting it all to work with software configuration (the really fun part starts now).
Check out this post on what others have done with streaming and the PI. My experiments showed that using GStreamer on both the PI and on Windows gives really good results with very low latency, if you use the right parameters.
Get GStreamer on the PI by following this blog. This is the same version of GStreamer that I am using on my setup.
Make sure your PI camera works ok by plugging in the PI to a standard monitor using the HDMI port and follow the instructions on the Raspberry PI website on how to get the camera up and running (without GStreamer). Once you have a working PI and camera, you can then proceed to stream things over the network.
Note: It is suggested that you first get the PI streaming video by plugging it directly into your local network where you can also connect your ground station PC with the correct IP addresses (without the Rocket M5). For my PI, I picked 192.168.1.2, and for the ground station, 192.168.1.1. Make sure you can ping the PI from your PC and the PC from the PI.
For streaming, you will also have to make sure all the ports you intent to use are open on the firewall (described later).
For the ground station PC, you can download GStreamer here. Make sure when you install, select to install everything , or full installation (not the default).
Here is the command I use for the PI to pipe the camera output to GStreamer:
raspivid -t 0 -w 1280 -h 720 -fps 30 -b 1700000 -o - | gst-launch1.0 -v fdsrc ! h264parse config-interval=1 ! rtph264pay ! udpsink host = 192.168.1.1 port= 9000
The command is explained as follows:
raspivid is the command to start the camera capture on the PI. The -w switch is for the width in pixels, and the -h switch is for the height. In this case, I am using 1280 X 720, but you can try any combination that fits your needs.
The -b switch is the bit rate for the sampling. In this case I chose 1.7mbs to send over the stream. Again you can experiment with higher or lower values. This settings seems to work good for me, and the latency is almost unnoticeable.
the "-o - |" is piping the output to gstreamer. Make sure you include the dash before the pipe "|" symbol.
For the GStreamer command, all the filters are separated with an exclamation point "!", as these are individual drivers that are part of GStreamer. Since the PI has hardware accelerated video, the output is in a format called "H264", which is a highly-compressed stream. The GStreamer filters are configured to transport the output via a UDP socket connection to the target PC. Notice the 'udpsink' element which specifies the host - in this case your ground station, and the UDP port. I am using port 9000, but you can use any open port on your system, but be sure to open the firewall or it won't work! You can also use TCP instead of UDP, but for such a data stream, I chose to use UDP since dropouts are certainly possible, and with UDP this is ok, but with TCP, you could have socket problems and higher latency.
Note: to get the PI to execute this command on boot, make a shell script with the above command and add it to your local.rc boot sequence. That way when the PI boots, you get the stream without having to log into the PI remotely.
For the ground station PC, once you have installed GStreamer and opened the correct ports, use this command (from the command prompt) to view the stream:
c:\gstreamer\1.0\x86_64\bin\gst-launch-1.0 udpsrc port=9000 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink
If all goes well, you should see the PI camera output on your PC screen in a popup window. For those of you what want to use FPV goggles, you can connect to the HDMI port on your PC to display the output if your goggles support HDMI.
I have this command in a batch file (with a PAUSE) statement at the end to keep the window open.
WHEW! If you got this far, you are amazing.
The last step to complete the build is to connect to the APM from mission planner. The method I used to connect was to install a utility that converts a TCP connection to a virtual serial port, but I also think that directly connecting the mission planner to the TCP port will also work, however I have not tried it. I will post back later after trying it.
Here is the link to setup the serial to ethernet device to have an IP address and port.
Here is the link to the configuration utility for installing the virtual serial port.
Once you have a serial connection over TCP/IP working to the APM, you should be able to connect with Mission Planner. On the maiden flight, it worked perfectly, and I didn't see a single drop in the telemetry data or anything noticeable in the video transmission, however my first flight was limited to 2km.
The last step is to connect the Rocket M5 to the Nano M5 and test everything using the OTA (over the air) connection. If all is well, you are ready to fly! But be careful on your maiden, you just spent $700.
Finally, here is a photo of my Antenna Tracker with the Nano M5 attached. My next update will include a video of a longer flight.
Happy Flying!
Comments
@Glen, I have a setup with a Quadcopter using the A+ Raspberry PI with a similar setup as you have described. I have this Netis WF2561 Wireless AC1200 500mW adapter, but the one you mentioned should work as long as the PI supports that adapter. Turns out I had to compile the driver for the Netis, but it was not too difficult.
Instead of using a separate lipo for the quad, I used one of the UBEC circuits from an ESC to power the A+. Turns out the A+ pi is low enough current draw for this to work, at least with the ESCs I am using. This will help cut down on the components. On another build I have for my hexcopter, I used a Raspberry PI 2 with a Logitec C920 camera, and this didn't work because it sucked too much power so I ended up adding a separate 5v regulator off of the main 4S battery. Originally, I had a separate lipo powering the camera and PI on my fixed-wing, but replaced it with a regulator.
For the ground station, the Nano M5 will work great. I have this setup mounted to a tracker, and have tested out to 2 miles with the hexcopter, but you have to use a tracker to get this range. I have a new setup using the nanobeam high-gain dish, but I have not gone beyond 2 miles with a copter because of my flight time limits, but I am sure it will go way beyond 2 miles.
I will be testing it with my fly-wing, possibly this weekend to see if I can get 10km+ with the dish antenna.
For your choice of antenna's on the UAV, I have found that a dipole works just as good as the planar wheel, unless you are using a spiral on the ground. I have experimented with both setups, but the spirals I was using had a lower gain than the patch antennas on the Nano, and the noise floor was better just using the Nano M5, rather than the Rocket M5 with custom spirals mounted on a tracker.
For the range you are wanting to achieve, there is no benefit in using a planar wheel over a dipole, and in fact, the stock antennas that came with the Netis adapter work just fine.
Now if you want telemetry, you can add this with no extra cost just by connecting your flight controller to the PI serial port and using a program like Ser2Net. If you are interested in how to configure your PI to forward your telemetry port, I can post the instructions.
Patrick,
I wanted to get your input as to slightly modifying this setup. I will be using a Q500 quad and my goal is to hit about 500-750 meters in range at 720p resolution with minimal interference using 5.8GHz (I would not be using the CGO2 camera when doing FPV). I do not need telemetry, just want a good HD picture with sub 200ms delay. My idea to cut down on components is to get an A+ Pi (and camera) and use a 5.8GHz USB WIFI adapter (Alfa AWUS051NH) as it appears to work well for broadcasting with the project Befinitiv has done. I would add a skew planar wheel antenna to it and power the Pi with the 5v power adapter you mentioned as well as a dedicated LIPO. Then I would get the Ubiquiti Nano M5 to make a connection to the Alfa and hook up a Windows laptop. So essentially I would set static IPs on the USB network adapter, M5 and Windows laptop and use Gstreamer to start the stream to the Windows laptop IP address and then have the Windows laptop receive it.
Does this sound like this could work out? I appreciate any insight you have on this.
@babak_ea, The reason you cannot play back is that the stream is missing the correct mpeg header. GStreamer will not write the header unless the pipeline receives and EOS (End of stream) event. Since the stream end is undefined, just stopping it on the PI won't write the EOS.
If you use the GStreamer HUD instead of the basic GStreamer window, you can force the EOS to be written by pressing the 'Stop' button while the stream is running. I have been recording my flights successfully using the HUD.
Hello Patrick
I success get stream video on network.
for save receive video to file use the following command :
udpsrc port=%d buffer-size=60000 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! tee name=record ! queue ! avdec_h264 ! video/x-raw, format=I420 ! d3dvideosink sync=false record. ! queue ! mp4mux ! filesink location = a1.mp4
when exit with Ctrl+c from CMD, the a1.mp4 does not play with media player our other software.
I think that the media file in this way does not close correct, am I right?
@babak_ea, if your video is freezing, make sure you are specifying a 'config-interval' in your gstreamer pipeline string on the PI:
something like this: .... rtph264pay config-interval=1 .....
I Use this video and the following command, then give live stream video
https://www.youtube.com/watch?v=lNvYanDLHZA
oh! big thanks Patrick!
I forget disable firewall of antivirus ,now disable and first run windows gstreamer then pi and receive one frame but freez and no stream the video, what happen?
@babak_ea, you may have an issue with your firewall on your PC. Make sure that the ports you are using are open on the PC side.
@patrick, it's very interesting.
but at first,I should solve network problem that explain at above post.
hello guys I'm new in raspberry pi. I build network Pi and window pc with cable and wifi,but both have same problem when run gstreamer command(according to first patrik post) on pi and pc,on pi side have camera stream on HDMI monitor, but on pc side have nothing! no error messege on pc side only display "New clock: GstSystemClock" Pi ip is 192.168.1.10 and pc Ip is 192.168.1.101 when ping pi from windows CMD pi response but when ping windows pc from PI command line it does not response.
is this necessary install and run service like NIS, Avahi or Samba on my PI?
Please guide me.