Monthly Archives: February 2014

Tutorial: How to create a webserver for robots, rovers, drones

The main scope of this tutorial is described in the title.

The architecture I have in mind is the following:

  • raspeberry pi is the main controller of a device (like car, robot, rover or in my specific case, a quadcopter).
  • raspberry pi is connected to a local wifi network.
  • My  pc or my smartphone are connectd also to the same wifi network.
  • Using the browser of pc/smartphone I want to pilot the device.
  • The development is done using python

To do this approach it is necessary a webserver running on the raspberry and at least a  web page as user interface.

Let’s start assuming I need to manage some parameters (for example the speed of the wheels, the angle of a robot arm).

First of all a created a generic  class  that  includes the parameters to be managed:

class mydataclass(object):

def __init__(self):

self.param1=0

self.param2=0

Second, I create my webserver.

The class is in the webserver.py module. It is managed as a parallel thread.

You can find an init() routine that get the data class as input and have a start() and stop() routine.You can use it as is, since is not related to the data structure of the mydataclass.

Then , I created a  class MyHandler(BaseHTTPRequestHandler). This is where it is necessary to implement  specific code in order to manage the specific parameters. In this example , there is a param1 that can be incremented or decreased  from teh user and a second param2 that is managed somewhere else (in the main loop).

It is implemented a  do_GET() routine  where it get the  command coming from the client (web page )  and according to the command, it performs the necessary action.

Finally I created a webserver_test.py module that  is used as main loop that mainly initialize mydata and  mywebserver and execute an example of basic control.

that’s it!

Ops, of course you need a (better than mine) web page for the command. In this link  you can find a really basic webpage and all the code: webserver_test.

To test it, just run:  python webserver_test.py

Than launch a browser and  open :  http://localhost/index.html   or  put the ip address where your server is running  :  http://192.168.0.10/index.html

In the next post I ‘ ll replace this basic example with a case specific for a drone.

Advertisements

Alfa3.test. Here it comes the python code

So now ,after some additional tests, here I added the software code: alfa3

All the basic components necessary to build a quadcipter sw are now implemented.
This is list of the modules developed:

  • motor.py
  • sensor.py
  • MPU6050.py
  • pid.py
  • rc.py

Any of those modules includes a specific object.I’ll plan some time to review the python page with a detailed help for each module.

Here I want just to summarise the main aspects.

rc is the new entry in this alfa3test session.it is an object that works in parallel thread.It is just waiting for input from user. In this particular case the input is coming from the keyboard,but the same approach it is possible (I’ m working on it and a tutorial is close to be ready) to get input from a webserver running on rpi.

Motor is an object that manages the motor speed trou ESC. It uses the RPIO library to generate the PWM.

Sensor is an object that works in a parallel thread. Every around 6ms it can update the info from the gyroscope and the accelerometer ,quadcopter inclination and rotational speed.

MPU6050 is a pure interface between raspberry and sensor hardware. If you want to use a different sensor it is just needed to build this specific class.

Pid is the object that includes the calculatio for the proportional , integral and derivative control.

Alfa3test. First tests and rc.py module

I did recently the first tests on alfa3.py.

I found some points where I have to work.

First of all the test structure is now limiting the quadricopter frame  to rotate without be affected by friction.So, when thw qpi is closed o zero degrees, The friction is higher and this riduce the rotationl speed of the qpi, so the system become more stable that expected.I have to review this part of the  structure.Maybe one test I can do is to increase the throttle, so the motor will push higer the qpi frame , reducing the friction.

Now the roll is mechanically limited at +/- 10 degrees.

alfa3_pic2

I now prefer to give more freedom on the qpi rotation, so I have to remove this constrain.

Let’s go now to the sw side. In alfa3 test I included a new module: rc.py (remote control).It has the target to leave indipendent the command input from the main control loop.So rc.py includes an object that can run as a parallel thread , that has the olny tak to wait for the user input. In this phaes the input is given by a keyboard, but I’m still already working on a web server that can receive input from any device with a browser (my smartphone , for example).

I’m not yet publishing the alfa3.py code , since the rc.py has a bug that I want investigate.There is  a delay between the input and the answer of the system.

Alfa3test. PID introductionand more…

Starting with this series of tests Alfa3,I added some new features that move the project to the final setup.alfa3_pic1In particular I add the following modification that allow the system to work wireless:

  1. I added a second ESC and a second motor.
  2. I modified the connector board adding a new 3 pin connector (C in the photo) where there is the zero volt (black wire),  the signal (white wire) that is bridged with the 4th signal (green wire), and also added the 5 volt (red wire) .connector_board In this way I can choose if rpi is powered by  the ESC (C connection) or not (B connection).
  3. So the schema for this test consist of the connection of motor M[0] in A and motor M[1] in C.
  4. I connected also my wifi dongle in the rpi usb port. I discovered that neither the pc nor the wifi dongle can act as access point. You can verify this using the comand:

iwconfig ap

So I used my smartphone activating the router wifi function,building up a network including the PC and the rpi.

Below the references used in the test:

alfa3_convention

  • M[0] turns counterclockwise, the red wire is connected to the “T” wire of the esc (the one that is closer to the T of the Turnigy logo),and the yellow wire to the “Y” wire , the black wire with the center wire of the esc.
  • M[0] mounts a standard left prop.
  • M[1] turns clockwise, the yellow wire is connected to the “T” wire of the esc ,and the red wire to the “Y” wire ,the black wire with the center wire of the esc.
  • M[1] mounts a right prop. (marked  R).
  • According to the position of the IMU,I have a negative roll rotation if M[0] moves down.

I did already some tests with this configuration. In the next post the first results and the description of th enew sw module rc.py that is the remote control module.