Sensor additional
MPU6050
MPU6050 is an IC with a 3-axis gyroscope, a 3-axis accelerometer and temperature sensor. It uses I²C to communicate. Here is an example code to use MPU6050:
// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include <Wire.h>
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop()
{
int16_t ac_x, ac_y, ac_z, temp, gy_x, gy_y, gy_z;
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ac_CEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 14); // request a total of 14 registers
ac_x = Wire.read() << 8 | Wire.read(); // 0x3B (ac_CEL_XOUT_H) & 0x3C (ac_CEL_XOUT_L)
ac_y = Wire.read() << 8 | Wire.read(); // 0x3D (ac_CEL_YOUT_H) & 0x3E (ac_CEL_YOUT_L)
ac_z = Wire.read() << 8 | Wire.read(); // 0x3F (ac_CEL_ZOUT_H) & 0x40 (ac_CEL_ZOUT_L)
temp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
gy_x = Wire.read() << 8 | Wire.read(); // 0x43 (gy_RO_XOUT_H) & 0x44 (gy_RO_XOUT_L)
gy_y = Wire.read() << 8 | Wire.read(); // 0x45 (gy_RO_YOUT_H) & 0x46 (gy_RO_YOUT_L)
gy_z = Wire.read() << 8 | Wire.read(); // 0x47 (gy_RO_ZOUT_H) & 0x48 (gy_RO_ZOUT_L)
Serial.print("Temp = ");
Serial.print(temp / 340.00 + 36.53); // equation for temperature in degrees C from datasheet
Serial.print("\tAcc X = ");
Serial.print(ac_x);
Serial.print(" Y = ");
Serial.print(ac_y);
Serial.print(" Z = ");
Serial.print(ac_z);
Serial.print("\tGyro X = ");
Serial.print(gy_x);
Serial.print(" Y = ");
Serial.print(gy_y);
Serial.print(" Z = ");
Serial.println(gy_z);
delay(333);
}
Further Readings
Try it yourself 03
MPU6050 Gyroscope
- Try to detect whether the MPU6050 breakout board is tilted using the gyroscope and/or the accelerometer readings.
Kalman filter
It is possible to calculate odometry using either gyroscope or the accelerometer readings. However, accelerometer is noisy and hence is inaccurate in short term; while gyroscope drifts and hence is inaccurate over long term. Here comes Kalman Filter to save the world. It fuses different measurements over time to provide a better estimation of the value. It is a difficult topic in robotics, so understanding and using of Kalman filter is not required at this stage. Just for your reference.
Understanding how the stuff works is a bit complicated for beginners. Thus, there are always geniuses which make a library by themselves. We can include the library so that values can be obtained by just calling the corresponding function. There is a Arduino library for Kalman Filer:
Try it yourself 04
kalman Filter
Install a Kalman filter library to the project and use it to filter the output of MPU6050
Try it yourself
clickclickclick
There was a game called clickclickclick.com, which players compete to make most mouse clicks in a week. People use different methods to fight for their victory. Unfortunately, it was closed in 2017, but we can still make a robot that click your mouse automatically to memorize this game.
- Use a servo to click a mouse automatically.
- As the user may want to temporarily use the mouse and move the mouse away, it should stop clicking when the mouse isn't nearby. Use a IR distance sensor to detect the presence of the mouse. You may want to use a mouse that's white in colour.
- It is dangerous if the robot still operate when it's falling! Stop servo action when it suspects that it is falling and restart only when there's a user reset.
Additional reading
CUI AMT-102V
Try it yourself 03
Motor speed sensing
- Connect the encoder disc and speed sensor to the motor, and connect the digital pin of speed sensor to Blue Pill. Use digitalRead() and rotate the motor manually to observe how it behaves.
- Drive the motor with the board and write a code to measure the Revolutions Per Minute (RPM) of the motor. You may use interrupt to count the number of or with the help of built-in function pulseIn().