Tag Archives: rc control

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.

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.