Category Archives: Uncategorized

myR: the rover module

Finally the last module is the module, where I put together all the development described in the past posts.

First, there is the initialization of all the objects , that are all working in parallel threads and sharing info by datacollector:

def __init__(self, name, M0b=18, M0f=23, M1b=24, M1f=25, T1=4, E1=17, simulation=False): = datacollector() = camera(
self.camserver = camserver(
self.sensor = us_sensor(‘S1’, T1, E1,
self.motorLeft = DCmotor(‘MLeft’, M0b, M0f, 11, 12)
self.motorRight = DCmotor(‘MRight’, M1b, M1f, 13, 14)
self.rc = rc(
self.server = server(
#self.speaker = speaker(
#self.display init in start() in order to read the output print from other classes

The basic functions of this modules are the ones used to move  the motors:ef brake(self):

def brake(self)
def right(self, time=0)
def left(self, time=0)
def down(self, time=0)
def up(self, time=0)

With time=0 the command stay until a new one is coming, otherwise the movement is done for the time value.

In order to  update the motor status  the updatemotors(self) is called.In this function it is checked which command is arrived from outside.

Consider an higher level, the rover can work in 4 different modes:

  1. jog mode . the user controls the rover and move it by arrow on the keyboard or buttons on the webpage (
  2. discover mode. The rover is moving around the floor and use the distance sensor to avoid the obstacles.The used rule is really simple. move forward until an obstacle is found (measured distance < min distance). Then turn left and retry to to move forward.
  3. search mode. the rover moves around the the floor and use the picam to search and move to the green ball places somewhere in the floor.
  4. program mode. the user define a list  of commands (move left, move right, etc.)  and the rover executes them.(not implemented yet)


The discover mode right now is the most interesting.

This module is under development.So up to now it take a picture, then if it found a green ball, it turns in order to have the ball in the center of the picture. Then move forward until the distance measured by sensor is < 100mm


What I’m working on right now is to use vision to estimate the ball distance. I can estimate the distance by a proportional calculation related to the dimension of the ball.I calibrate the camera to obtain distances in this way.

I took a picture  with the ball positioned in a defined  distance(meas_distance in mm). Then I got from the system the ball radius (meas_radius in pixel).Finally I have the proportion:


and so:

current_distance = (meas_distance*current_radius)/meas_radius

Then I want to add more intelligence in the algorithm , for example if an item is not found, I can rotate and take a new picture.


myR: Let’s put all together

Now it is time to put all the hardware together.


I suddenly figure out that there can occurs some troubles. In particular my usb wifi dongle sucks a lot of current.

My original design was that a 4xAA Battey pack  can power the rover. So I connected the batteries to the L298N bridge and connect its 5 v output to RPI. But when the rover was connected  via wifi and it starts a motor , the rpi just shut down ( not enough current: the L298N bridge output can give only 0.5 A).

Then I try by using a BEC from battery directly to the rpi: same result.

Finally  I adopted a quick and dirty solution: put a second battery pack , one used to recharge my tablet. (just remember to  put the 2  grounds in common)


In the next post we will talk about the sw.

myR: display

The display module is a basic UI done in the console to see the main information coming from the rover.


Just using the datacollector module, It is possible to obtain all the data from any module.

In the example , I included also the sensor and the rc module runing in parallel.

Use this to test it:

sudo python

You can use arrows to change direction , 1-4 to change the modes (see next post for description), a to increase speeed and z to reduce it, q for quit (horn and speak is not used).

myR: datacollector

We need to take a little step back introducing  the concept of the datacollector class ( in module).

Probably you notice that for each class I always added a complementary class called  something_data (for example rc class and its  rc_data).I decide to  use this approach to have a common and structured way to get access to all the data  from any class.

The datacollector class is a class that inherits from all the data class of my objects.

class datacollector(rc_data, us_sensor_data, speaker_data, server_data, camera_data):



Let’s do the example of the data input.

Using the browser I can  send a command (for example “move left”) to the rover by storing it in the server module as


In the rc module I can read the , BUT since this info has got the same name in the 2 modules and it is defined in a  common class called datacollector, the 2 data.input are the same thing (the same pointer to the memory).

If, for some reason, I would like to add an input coming, for example from the sensor,  it could be really simple: just add a data.input in the sensor_data .And without any modification in rc module, the rc module can manage the  new input coming.

Again, in my main class rover  I can access all the data from all the classes just using or or

I hope I have not been to philosophical this time…

myR: server

In myR project there is included a web server in order to communicate with user. The main advantage of this approach is that I can use any device that can run a web browser to get information about my rover and I can also control it from the browser.

I’m using tornado as webserver, and in particular it can provide 3 applications:

  • MainHandler. It provides  the index,html page, the main page, including the camera streaming, the movement buttons and the additional  command buttons.
  • ConfigHandler. It provides the config.html page, used to tune the HSV parameters for the camera.
  • WSHandler. It provides the web socket communication from the webpage to the rover. This is done to a fixed IP address in order to guaranty a fast connection.

You can find this development in the module.

In order to get the main page, start the server  by typing:

sudo python

Then on your browser type:



In order to see the configuration page , type:



Note that since in the example the camera server is not present, you cannot see the camera streaming.

In git-hub I also added the webpages used.

myR: Remote Control

So I completed the hardware,  let’s now move to the sw part of  project.

The remote control has a very simple scope: receive a command (data.input) from somewhere , check it and store it in the appropriate location , mainly a data  available for the other  objects.Again, this object is running as a parallel thread ( I love parallel threads…).

The code is in  and  modules.




myR: The Pi Camera p2

camserver and CamHandler

in order to stream the video on a  browser , I add this camserver object that it takes care to pass the last available image.

Consider this webserver is working on port 9094, You can easily test it by  run:

sudo python

than typing on a browser:


and (you need to close the previous page first)

They are respectively the last image with result of ball tracking and the result of the filters before search for the contors.

This can help to define which values of HSV are correct to look for green color.This part can be quite tricky, so I created also a html page that can help me by moving slides on it.You can open it in the browser but for the moment it does not apply the change on the mask (this will work only when I will introduce also the and the -remotecontrol- in the future posts).


In my github you can find now the upload of this part of the code.

PS: Note that now there is a bug that I did not yet solved: since in the camHandler.do_GET there is an infinite loop continuously updating image, you can not open two pages  connected to the same server.You need first to close a webpage than open the new one.


myR: How to control a DC motor

So let’s start  by moving the rover!

In my case I have 2  DC gear-motors with this specifications:

  • Gearbox: 4.8~7.2V
  • No load current:190mA(max.250mA)
  • Stall current: ~1A
  • No load speed: 90±10rpm
  • Torque:

The control of this type of motor is really simple: when power on, it moves, when disconnected it stay still. Simple!

So what we can do is tuning the motor speed by  power on and off the motor itself using pulses of different width: we  will use the PWM (pulse width modulation).


So consider  to send a pulse every 20ms (subcycle=20ms)  and  the pulse width is 16ms, than the motor stays on for the 80% of the time, while if the pulse width is 10ms it stays on for 50% of the time.


The motor can drain up to 250mA .It is too much for our raspberry output, so we need  a  bridge  in between than can guaranty this current.

That’ s why in the Bill Of Material I have the L298N bridge.

This board receive the input from rpi and send an output of the same width , with a signal level (voltage and current) depending on the power in  you connect ( the used battery pack).

In particular , the bridge I’m using can drive up to 2 motors.


Below  the schema for the power connection. Note that the bridge can also provide a 5 V output than can be used to power the raspberry.


In the next post, I’l explain how to connect the outputs and how to control them by software.


myR: Under the Christmas tree…

Under the xmas tree,  Santa put a  present for me including:

  • Dagu 4D Magician Chassis (including 4 DC motors, and  2 plastic plates,4 x AA battery holder) -21 Eur
  • HC-SR04 sensor (a ultrasound sensor for measuring distance) -4 Eur
  • L298N bridge ( a bridge for controlling up to 2  motors) -4,5 Eur

In addition I had already:

  • raspberry pi
  • Pi cam
  • a wifi adapter
  • an omniwheel (spherical)
  • an empty  tic-tac candy holder
  • 4 X aa Batteries
  • a smartphone battery pack (2.5 A)

So I put every togheter and it pops out myR:  a super Rover!!!


Nothing special under the sky, but an interesting project  to test autonomous veicles. So, to reach that goal , I’m now working on  a rover that can act in this 4 modes:

  • Jog mode, moved by operator ( my 5 years old son…).
  • Program mode: it is possible to create a sequence of movements and the rover can repeat them.
  • Discover mode: the rover can move randomly aroud the appartment avoid any obstacle in fornt of him.
  • Search mode: the rover can search and reach a ball placed or moved around.

As always I’m developing this features using python and  using object-oriented programming: for each item I create a module that implements all  the necessary features for this item.

In the next weeks I’ll post the development steps.

Happy new year and Keep in touch!

Are you still updating the blog?

In the last months I received  questions from followers  and from private e-mails about the state of  my project and this blog.

I reduced to zero the development mainly for the birth of my second baby. In addition my new job position did the rest : no more nights to dedicate to  my quadcopter.

I just  try a couple of time to fly : in  the second time in particular I obtained a broken prop…

By the way, It is really exciting to me, see so many contacts daily evenif I’m not for 6 months.

This give me new energy to restart.

So I cannot promise it , but I’ll try to be back on track!