Friday, 11 September 2015

IP socket communications

I'm putting together this simple C#-based Windows Forms control app to interact with the quadcopter (written in C++, compiled for ARM). The app will send the flight control commands (e.g. manual input from the Xbox One controller, or waypoints once I have GPS integrated), and receive telemetry data from the quadcopter (e.g. inertial measurements).

At this point I've opted for socket-based IP communications (I'll decide if it's worth it to migrate to long range RC based protocols later) as it's easy and straightforward. There are 3 sockets, each used on its own thread to take advantage of the 4 cores on the Pi
  1. Beacon socket thread: broadcasts a UDP message, every 1 second, advertising the IP of the quadcopter on the network. Used by the control app to detect and connect to the quadcopter.
  2. Incoming command socket thread: TCP socket - in order to insure reliability of connection - used to receive commands from the Xbox controller and the control app. This thread will update a singleton class that contains the flight state and current controller input, accessible by other threads.
  3. Outgoing telemetry socket thread: UDP socket - don't care if packets get lost, just need to minimize overhead. This socket will be used by the control thread, the same that samples the sensors and adjusts the motor speed. I might also run it on a separate thread and queue the telemetry data points to send in a FIFO - whichever has the least overhead on the control loop.
Everything has been implemented in a class inheriting a virtual interface so, assuming long-range RC protocols like Xbee are not too different, I should be able to switch implementation without having to touch the rest of the codebase.

So far everything works fine :-)

As usual the code is on bitbucket. I'll make the control app source public as well soon.

The control app UI is now used to plot the Xbox controller input and send it to the Raspberry Pi. The Pi then only takes the throttle value and changes the PWM signal on channel 0 of the PCA9685.

The charting section of the UI will also be used to plot incoming telemetry. It's going to be useful to calibrate and implement the right low-pass filter on the inertial unit.

 
The next step will be to interface with real motors once I receive them and the ESC/battery.


Thursday, 3 September 2015

BOM Update

I've updated my bill of manufacturing as I locked it and ordered the parts.

Description Size Units Weight (g) Wratio Price (CAD) Pratio Req. TWeight Tprice
Structure                  
Polycarbonate Sheet 432 sq-inch 1000 2.3  $        23.90 0.06 128 296 7.1
Goujon SQ 1/2 48 lin-inch 99 2.1  $          1.78 0.04 48 99 1.8
Goujon RD 1/2 48 lin-inch 44 0.9  $          0.98 0.02 32 29 0.7
Insulation tube 36 lin-inch 17 0.5  $          1.29 0.04 6 3 0.2
Sorbothane 1/4" 30 DURO 50 sq-inch 50 1.0  $        26.95 0.54 8 8 4.3
Hardware (screws, nuts, etc.) 1 bulk 100 100.0  $        50.00 50.00 1 100 50.0
Propulsion/Energy                  
750-90008 KA 1050Kv Brushless Motor 1 motor 57 57.0  $        35.09 35.09 4 228 140.4
APC Propellers 10x4.5MR/MRP (ST) 4 unit 65 16.3  $        17.00 4.25 4 65 17.0
30A SimonK  ESC  4 unit 91 22.8  $        45.80 11.45 4 91 45.8
Bias 40C 3S 5000mAh 11.1-volt Lipo  1 unit 415 415.0  $        67.00 67.00 1 415 67.0
Control                  
PCA9685 (PWM) 1 unit 7 7.0  $        15.85 15.85 1 7 15.9
Raspberry Pi 1 unit 45 45.0  $        46.11 46.11 1 45 46.1
MPU9250 (accel/gyro/compass) 1 unit 5 5.0  $        15.61 15.61 1 5 15.6
BMP280 (baro/altitude) 1 unit 2 2.0  $          9.95 9.95 1 2 10.0
Rangefinder LV-EZ4 Maxbotix  1 unit 4.23 4.2  $        24.95 24.95 1 4.23 25.0
MCP3008 (ADC) 1 unit 1 1.0  $          3.75 3.75 1 1 3.8
(optional) GPS
(optional) Xbee radio

I've updated the target cost as I found the quadcopter reference price in CAD.

Metric Total Target Delta %
Weight (g) 1399 1500 -101 -7%
Price (CAD) 450 742 -292 -39%

Flight dynamics basics - yaw, roll and pitch

Basic quadcopter flight dynamics were not complex to understand.
You need four propellers, two of them spinning clockwise, and the other two spinning counter-clockwise - in order to have a net angular momentum of 0.

Specific terms are used when the quadcopter rotates around the 3 possible axes.

Roll and Pitch

If you want to tilt the quadrotor left/right (roll) or forward/backward (pitch), then you need to have motors on one side spin faster than motors on the other side.

Yaw

If you want to rotate the quadrotor around the z-axis, then you need to change the net angular momentum to be positive or negative, by increasing the throttle of both motors rotating in the same direction (CW or CCW).

Equations

To make sure that the net thrust of the quad remains stable when rotation/tilt is applied, the desired rotation speed increase is divide by two. That offset is then added to one motor and subtracted from the motor on the other side.

Using XBox One Controller for command input

One of my requirements was to achieve fully autonomous flight. It dawned to me that before I get there, I'll probably need to manually control the quad to perform flight tests and improve the control subsystems. Hence I need a controller, but I don't want to invest in a costly radio-controlled solution.

I already have WiFi onboard the Pi. I have an Xbox One Controller. And I already need to develop a control app to run on my computer. I'll simply interface with the Xbox One controller, which is extremely versatile.


The control mapping will surely change, but it could look like the above.
  • Throttles: control motor throttle in manual mode, control altitude in altitude stabilization mode
  • Select: switch between manual, altitude stabilized, position hold and fully automated
  • Start: start/stop onboard control
  • Right joystick: adjust yaw
  • Left joystick: adjust pitch (up / down) and roll (left / right)
  • A-button: automated lift-off and landing activation
The controller can be plugged into any Windows system with a USB cord and is instantaneously recognized. Plus there are DirectX APIs to interface with it. Since I'll be developing in C# on the Windows side, I'll be using the SharpDX.XInput library, a convenient managed wrapper around the DX native XInput APIs.

I'm using Visual Studio Community 2015. In the package manager consoler, install the latest beta version of SharpDX.XInput
Install-Package SharpDX.XInput -Pre
I'm using the beta version as the latest stable release is based on DX11 and I ended up with missing DX DLLs (Windows 10 comes with DX12).

Once you have that imported, getting a Controller object is quite simple.. In the example below, in a simple Form app, when I update the text field with the X/Y coordinates of the left joystick anytime the form button is pressed.

using SharpDX.XInput;

namespace QuadRemote
{
    public partial class Form1 : Form
    {
        public Controller _controller;

        public Form1()
        {
            InitializeComponent();
            _controller = new Controller(SharpDX.XInput.UserIndex.One);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_controller.IsConnected)
            {
                var state = _controller.GetState();
                var x = state.Gamepad.LeftThumbX;
                var y = state.Gamepad.LeftThumbY;

                richTextBox1.Text = x.ToString() + " " + y.ToString();
            }
        }
    }
}