Monthly Archives: March 2016

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.