# SW Dev: Math model for control loop

Before to start development of the python code, I tryied to create the math model that can reproduce the theoretic dynamic behaviour of a quadcopter.You can find it in  control_loop.xls.[Rimoved. See here]

Please, if someone interested can send me its own quadcopted data (length motor-quad center,arm mass,motor mass,total mass,hover motor speed,PID parameters)I’d like to put in there to validate my model.

Hypothesis:

• configuration +copter
• case study: Roll

Data:

• R.target is the desired roll angle
• R.acc is the angular acceleration(calculated respect the angular motor speed w of the previous )

Dynamic balance is given by:

(F1-F3)b=I R.acc Kprop(w1^2-w3^2)b= I R.acc

with Kprop=(Mtot*g)/wh^2   I=(Mbar*b^2)12+2*Mmotor*b^2

R.acc=Kprop*(w1^2-w3^2)b/I

• R.vel is the angula roll speed calculated as integral of R.acc
• R.curr is the thoretical roll angle calculated as integral of R.vel. This value should correspond ot the return of a gyroscope mounted on board of the real quadcopter.

So using this model I can play with P,I,D parameters and find out teh best configuration.

# HW Dev: Frame

This week I was concentrated on the frame of the quadcopter.

I found in a bricolage center this specific cross for mounting aluminium bars togheter.It was available only the format 20x20mm. So I bought it togheter with one meter of alu bar.

I cutted 4 pieces by 250 mm and I mounted in a second.

I drilled the 4 arms  for the mounting of the motors and for fixing the cross.

The motor fixture is drilled for a M3 screw, so I adopted for all the frame the same screws: 12 screws M3 of 30mm.

Note: To be sure that I can remount the arms always on the same position I mark the cross and the arms with a cutter.

Prices:

• Bar: 5.90 EUR
• Cross: 5.40 EUR
• Screws: 0.8 EUR (25pz)

# SW Dev: Intro controller loop

So far the sw development included basic functionality of jogging going to directly modify the speed engine and leaving the user to their modulation.

At this point I want to introduce the concept of control in which the ‘operator indicates the’ desired angle and the controller, our rapberry pi, must decide the speed ‘of the engines.

The theoretical relationship between the quadcopter angle (be it roll, pitch, yaw, or a combination of them) can be found on the page of the quadcopter. The balance that you get is unstable and just a minimal disturbance (such as a breath of wind) can cause this balance is lost.

In reality we must also consider asymmetries of construction (structure, propellers, etc..), Or non-constant behavior of the motors.

That translates into the need to know:
• what ‘s the instantaneous behavior of the aircraft
• what ‘s the real relationship between angles and speed engines as different from the theoretical

For the first point we must introduce something that can read the current state of the quadcopter, say a sensor. In most cases, a sensor mounted on board as a gyroscope or gyro+ accelerometer.

The gyro returns the angular velocity from which the integral angle is achieved. The accelerometer returns l’ acceleration in its components x, y, z

Thus, known the current angle and the  target angle,it  can be possible to calculate the error and decide a strategy to modulate the speed of the engines.

This strategy results in a control method called PID.

It is assumed that the relationship between two variables (in our case motor angular speed and angle) is approximated to three components:
• Proportional (P) = kp * Ei
• Integral (I) = Ki * sum (Ei) * t
• Derivative (D) = kd * (Ei-Ei-1) / t

Where Ei and ‘target error between w and w ith current instantly, you’ cycle time of the loop where the various k must be determined empirically.

The pid so returns the variation of the motor speed in order to minimize E.

A loop that reads from the sensor, calculates the  correction with pid,applies the motor w is the basis of the angle control loop

In the future posts I will dedicate a specific description of the following blocks: sensor, PID and  control loop with the purpose of defining the final structure of the program.

# SW Dev 2: quadcopter.py

Hello ,  I’m publishing the new module quadcopter.py.

This object  has the main scope to be the controller of the quadcopter.

It includes 4 motors (from motor.py class) and the necessary interface for manage the commands coming from user.

This version is preliminary since it does not iplements all the functionality  but it can be considered the first step for the qpi logics.

Let’s see in details what is included in the modules for qpi_test2

motor.py is getting more robust. i added some procedures to manage the motor  parameters like setSimulation and getSimulation.

I also added a check to verify if the RPIO library is available.In case it can work anyway in simulation mode.

Procedure  motor.start  implements a sequence for initialize the ESC (it must to be verified yet)

Procedure motor.stop  that stops immediatly the motors.

quadcopter.py can manage 4 motors. the motors are defined as a list (array) so it is possible to direct access any  motor  with its index, for exemple:  self.motor[1].start

it includes procedures to move the quadcopter, like increaseThrottle,decreaseThrottle, increaseRoll,decreaseRoll ,increasePitch,decrease Pithc, increaseYaw,decreaseYaw and hover.

For the moment those procedures are used directly on the motor speed, but in next releases they will work  on angles and the conversion has to be managed

For this reason this class can be used only for development activity to have an overview of the whole flow.

qpi.py is  again the user interface, but i added a minimal graphical interface.

it is possible start the motors by pushing 0-1-2-3

• increase motor rotation with a-z , s-x,d-c,f-v
• play with throttle y-n, roll i-m, pitch j-k yaw g-j, hover h

you can test it even if no motor is connected, but also in a pc running linux, different from raspberry pi

# SW Dev 1: motor.py

This is the first post dedicated to the sw development.On page  Python you can find the different sw versions and the description of the used classes.

Here the  link   for download the files motor.py  and qpi_test1.py  used to pilot motor M0.

The module qpi_test1.py is the basic user interface.

The module  motor.py  implements the object “motor”  that manages the angular speed of the motor and the hardware interface between raspberry pi  and the ESC.

the angular speed is defined in % instead of absolute value:

1. to keep motor object independent from real used motor
2. to keep motor object independent from real used battery (remember that motor spped is a function of teh voltage).

Motor parameters are:

• current angular speed (RPM)
• Min and Max angular speed (RPMMax,RPMMin)
• The speed that balances the gravity (hovering)  (RPMEquil). This value can vary according to different factors , like altitude, air temperature, humidity, qpi weight.So it is necessary to tune this value every new flight increasing the throttle upto equilibrium than save this value and reuse it when necessary (for example after a roll).
• the pin where I connect ESC on rpi GPIO. here  the circuit diagram.
• The motor Kv. Right mow not used but it could be used in future to estimate real angular speed or the power consumption.
• debug mode, boolean that define the debug mode
• simulation mode,boolean that activate the PWM (=TRUE di default)

If you want to pilot the motor it is necessary to remember 2 things:

1. The used ESC could request a startup procedure ( See ESC)
2. Set  myMotor.simulation=FALSE

The I/O interface is based on RPIO Library.

By default it works at 50 HZ ( a pulse every 20 ms ).

The pulse width is the command to the motor.

With a pulse width of 1ms =1000us  , the motor stops.

With a pulse width of 2ms =2000us  , the motore  rotates at maximum speed.

The increment can be of  10us.So the pulse width PW:

PW=(1 + (self.__RPM / 100)) * 1000

self.__IO.set_servo(self.pin,PW)

(with   self.pin=18,  it means  GPIO18, and physical pin 12)

Link:

http://pythonhosted.org/RPIO/pwm_py.html

# SW Installation

Hello, today I inaugurate the english version of my blog.

As soon as I can I’ll translate also the static pages.

By the way, consider that this project is based on raspberry pi (rev B) for the motion control of the quadcopter.

Here there is a description of the necessary sw installation.

I’m using a laptop with ubuntu, and there I installed  ninja IDE, direcly from  ubuntu software center.

On this laptop I create a folder home /home/pyton/qpi.

Considering the rpi installation:

I copied a 2013-02-09-wheezy-raspbian.img on a  2GB SD.(right now you can find an updated verison of raspbian, on the rpi website  here: link

Start rpi connected on a monitor,my case was tv  via hdmi wire , mouse and keyboard and a ethernet cable.

Leave the default setting in the config.

Now you can disconnect all from rpi except the ethernet wire.

On the router  I defined a static address :192.168.0.50 , related with rpi mac address. (i have a netgear router and it waas really intuitive to set it ).

From the laptop , on the terminal use [See note below if you encounter problem with ssh]:

```ssh pi@192.168.0.50
password : raspberry```

Now yuo are using rpi from the laptop

Execute:

```\$ sudo apt-get undate
\$ sudo apt-get upgrade```

The update/upgrade can take up to one hour.

Since I ‘ll use rpi to pilot the motors I ‘nned to install the library that can manage the GPIO in rpi.

This library  RPIO allows to set any output as PWM:

```\$ sudo apt-get install python-setuptools
\$ sudo easy_install -U RPIO```

Now let’s see how to set up the Wifi Connection via usb dongle.

Do not connect the usb dongle.

Install the  wpa_supplicant  utility:

`\$ apt-get install wpasupplicant`

Run the utility to create the password_phrase:

`\$  wpa_passphrase My_WiFi_SSID mypassword`

Yuo will get this output.

```network={
ssid="My_WiFi_SSID"
#psk="mypassword"
psk=b2abb0fcd2f4527e11817de0823a57bb19ba4622f4595062c94ec4dd1370b5fe
}```

Copy the  psk value.

Run the editor to modify the  /etc/network/interfaces

`\$ sudo nano /etc/network/interfaces`

Modify the file (pasting the psk value):

```...

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "My_WiFi_SSID"
wpa-psk b2abb0fcd2f4527e11817de0823a57bb19ba4622f4595062c94ec4dd1370b5fe```

Switch off  RPi.

Connect teh dongle and disconnet the ethernet wire.

Restart rpi and you are on line.

Set the router in case you want a fixed ip address for the wifi connection.

Link:

ninja IDE

RPIO

NOTE: recently I clean up my laptop and installed ubuntu 13.10 on it.

From that moment it has been not possible to use ssh using wired connection.

So I solve it modifying the rpi eth0 configuration manually.

pi@raspberrypi ~ \$ sudo nano /etc/network/interfaces
iface lo inet loopback
#iface eth0 inet dhcp
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1

In the same way, in the laptop I defined a static ip address 192.168.1.1