Tag Archives: sensor

myR: ultrasound sensor

The ultrasound sensor is used to measure the distance respect an item in front of it.

A basic ultrasonic sensor consists of one or more ultrasonic transmitters (basically speakers), a receiver, and a control circuit. The transmitters emit a high frequency ultrasonic sound, which bounce off any nearby solid objects. Some of that ultrasonic noise is reflected and detected by the receiver on the sensor.

WP_20160212_002

There are a bounce of tutorial on how to use it.I  just follow this perfect tutorial :

www.modmypi.com

In this post I want to focus the sw code I developed and I just post the wiring:HC_SR04_2

The module is called us_sensor.py and it is upload in  Github>>myRover.

Inside you can find an class object called us_sensor_data, that collect the information to share with the world (the distance in [mm] and the period of  measurements in [sec].

This choice to create a separate class will become very useful when I will put all togheter (in the rover.py). In this way any  object can see all the information from other objects.

 

The main class  is the us_sensor  object.

def __init__(self, name, trigpin, echopin, data):

Where you need to pass a name, the GPIO pin for the trig signal (output) and the GPIO pin for the echo (input) signal.

This object  works in a parallel thread (threading.thread), so while in your main loop you can do something, the us_sensor continuously (every data.period time) measures the range and put this information in data.distance.

Since it is parallel thread you need to start it first,by calling  mySensor.start(). This call recalls then the run(), where there is the measurement loop.

Then you need to stop it when closing the application , and mySensor.stop().

Note that if you have more threads running  the measurements become less precise.

In addition if an item is very close to the sensor it happens to loose some echo signal.In order to avoid the loop to freeze waiting for a signal that was lost, I manege also a timeout.

Finally you can find a main()  to test the sensor by itsself.

#init data class
data = us_sensor_data()
#init sensor
mySensor = us_sensor('HC_SR04', 4, 17, data)
mySensor.start()
#from this moment the sensor is measuring and puts the result in mySensor.data.distance
...
while mySensor.cycling:
     #here do something...
     ...
     #than get the info when you need it
     s = 'Distance: ' + str(mySensor.data.distance)
     ...
finally:
# shut down cleanly
...
mySensor.stop()

 

 

 

 

Advertisements

IMU Sensor Installation

Since a week ago, I ‘ve been looking into my new IMU: Inertial Measurement Unit.

I bought  from e-bay a MPU6050, a sensor including:

  • a gyroscope that can return the angular speed around the 3  axis x,y,z.
  • an accelerometer  that can return the linear acceleration along the 3  axis x,y,z.

This sensor can communicate trou I2C interface.

Below the electrical drawing:

wiring_IMUIMU_conn

 

 

 

 

 

 

 

For the sw installation I fuond a clear tutorial on the Adafruit website ,so I just report here a memo for the necessary steps:

in sudo nano /etc/modules add:

  • i2c-bcm2708
  • i2c-dev

sudo apt-get install python-smbus

sudo apt-get install i2c-tools

in sudo nano /etc/modprobe.d/raspi-blacklist.conf comment:

  • #blacklist spi-bcm2708
  • #blacklist i2c-bcm2708

Now ,running the command sudo i2cdetect -y 1  yuo have to see the address 0x68, corresponding to the default sensor address.

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.

control_loop

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.