Instructables
Picture of Gyro Camera for Motorcycle
gyro02.jpg
gyro01.jpg
As seen in MotoGP Race, the rider is seen riding through corners while laying aside his bike to the left and right. But there is an interesting moment when the motor looks to collapse sideward, the front views remain horizontally. How could that be?
Such onboard camera applies GYRO system, where the camera will be fixed perpendicular to the gravity of the earth.
 
Remove these adsRemove these ads by Signing Up

Step 1: BUILD YOUR OWN GYRO CAMERA

Picture of BUILD YOUR OWN GYRO CAMERA
gyro04.jpg
gyro05.jpg
gyro06.jpg
We could build our own Gyro Camera by using GYRO and ACCELEROMETER modules.
They are two separate modules, hence we have to use two modules simultaneously. Then we make Gyro Chip and Accelerometer Chip in one module (there are two chips in one module). In latest version they are made in one chip only, thus minimizing the distortion of movement calculation

In this article, the module is Triple Axis Accelerometer & Gyro Breakout – MPU-6050, which has 3-axis gyroscope and 3-axis accelerometer in one chip, supplied by power of 3.3volt.

In addition to module MPU6050, the following similar modules could also be applied:
• IMU Fusion Board – ADXL345 & IMU3000
• IMU Digital Combo Board – 6 Degrees of Freedom ITG3200/ADXL345

Module MPU6050 with its tiny size of 20mm x 15mm and height of 1.6mm.

The components are:
• Triple Axis Accelerometer & Gyro Breakout – MPU-6050
• Arduino UNO R3
• Digital Servo (use good and powerful servo)
• Breadboard Mini
• 9v Battery + Switch
• Box and other accessories.

If you using different board than Arduino Uno R3, SCL and SDA pins of MPU are also different:

VDD : +3.3V
VIO : +3.3V
GND : GND
SDA : Pin A4 (Arduino Uno, Ethernet) / Pin 20 (Mega2560, Due) / Pin 2 (Leonardo)
SCL : Pin A5 (Arduino Uno, Ethernet) / Pin 21 (Mega2560, Due) / Pin 3 (Leonardo)


Step 2: PROGRAMMING

Picture of PROGRAMMING
After having completely assembled, it is time now to upload the program to Arduino.

This circuit is only to drive servo in axis-X only. However, data from Axis Y and Z are still required for the respective Gyroscope and Accelerometer. I tried to combine them by applying Kalman Filter calculation so as to reduce ‘noise’ output from Gyroscope + Accelerometer so that servo movement is smooth and no unwanted movement.


CODE:

/*
  GYRO CAMERA - saft7.com

  Demonstrates auto-leveling Camera Video by using Gyro & Accelerometer with Arduino

  The circuit:
  Servo controlled by Arduino, using Gyro and Accelerometer as reference of movement.

  Created March 12, 2013
  by Firmansyah Saftari
  www.saft7.com

  This code and complete article can be found at:

  http://www.saft7.com/
 
  Programming Language: C++
 
  */


#include <Servo.h>
Servo xservo;

#include <Wire.h>
#include "Kalman.h"
Kalman kalmanX;
Kalman kalmanY;

uint8_t IMUAddress = 0x68; // MPU6050 Address

/* IMU Data */
int16_t accX;
int16_t accY;
int16_t accZ;
int16_t tempRaw;
int16_t gyroX;
int16_t gyroY;
int16_t gyroZ;

int moveX;
int mapX;
int correctionX;

double accXangle;
double accYangle;
double gyroXangle = 9;      
double gyroYangle = 180;
double compAngleX = 90;    
double compAngleY = 90;
double kalAngleX;
double kalAngleY;
uint32_t timer;




// ----------  VOID SETUP START -------------- /
void setup() { 
  Serial.begin(115200);
  xservo.attach(10);
 
 
  Wire.begin(); 
  i2cWrite(0x6B,0x00);           // Disable sleep mode 
  if(i2cRead(0x75,1)[0] != 0x68) {   // Read "WHO_AM_I" register
    Serial.print(F("MPU-6050 with address 0x"));
    Serial.print(IMUAddress,HEX);
    Serial.println(F(" is not connected"));
    while(1);
  }     
  kalmanX.setAngle(90);  // Set starting angle
  kalmanY.setAngle(90);
  timer = micros();
}

// ----------  VOID SETUP END -------------- /


// ---------------------- VOID LOOP START -------------- /
void loop() {
  /* Update all the values */
  uint8_t* data = i2cRead(0x3B,14); 
  accX = ((data[0] << 8) | data[1]);
  accY = ((data[2] << 8) | data[3]);
  accZ = ((data[4] << 8) | data[5]); 
  tempRaw = ((data[6] << 8) | data[7]); 
  gyroX = ((data[8] << 8) | data[9]);
  gyroY = ((data[10] << 8) | data[11]);
  gyroZ = ((data[12] << 8) | data[13]);
 
  /* Calculate the angls based on the different sensors and algorithm */
 
  accYangle = (atan2(accX,accZ)+PI)*RAD_TO_DEG;  
  accXangle = (atan2(accY,accZ)+PI)*RAD_TO_DEG;  
  double gyroXrate = (double)gyroX/131.0;
  double gyroYrate = -((double)gyroY/131.0); 
  gyroXangle += gyroXrate*((double)(micros()-timer)/1000000); // Calculate gyro angle without any filter 

  gyroXangle += kalmanX.getRate()*((double)(micros()-timer)/1000000); // Calculate gyro angle using the unbiased rate
  compAngleX = (0.93*(compAngleX+(gyroXrate*(double)(micros()-timer)/1000000)))+(0.07*accXangle); // Calculate the angle using a Complimentary filter
 
  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros()-timer)/1000000); // Calculate the angle using a Kalman filter
  timer = micros();
  mapX = map(kalAngleX, 0, 200, 0, 179);     //calculate limitation of servo mechanical
 
 
// /////////////////////////////

    correctionX = 27;     // EDIT THIS VALUE FOR SERVO CORRECTION ANGLE

// ////////////////////////////


  moveX = 270 - (kalAngleX) + correctionX;
 
// ------- SEND TO SERIAL PRINT START ----- /

  Serial.print("saft7.com X Pos: ");
  Serial.print(moveX);Serial.print("\t");
  Serial.print("\n");
 
// ------- SEND TO SERIAL PRINT END ----- / 




// ------- SEND TO SERVO START ----- /

   xservo.write(moveX);   // Send signal to servo
   delay(15);     // delay to allow servos to move (ms) 
  
// ------- SEND TO SERVO END ----- /


 
  delay(1); // The accelerometer's maximum samples rate is 1kHz
}
// ---------------------- VOID LOOP END -------------- /



// -- FUNCTIONS START --
void i2cWrite(uint8_t registerAddress, uint8_t data){
  Wire.beginTransmission(IMUAddress);
  Wire.write(registerAddress);
  Wire.write(data);
  Wire.endTransmission();                       // Send stop
}

uint8_t* i2cRead(uint8_t registerAddress, uint8_t nbytes) {
  uint8_t data[nbytes]; 
  Wire.beginTransmission(IMUAddress);
  Wire.write(registerAddress);
  Wire.endTransmission(false); // Don't release the bus
  Wire.requestFrom(IMUAddress, nbytes);   // Send a repeated start and then release the bus after reading
  for(uint8_t i = 0; i < nbytes; i++)
    data[i] = Wire.read();
  return data;
}
// -- FUNCTIONS END --

// GYROCAM BY SAFT7.COM //

// END

Step 3: Build The Camera Bracket

I use 2 mm Acrylic 2mm, bent by heating. Make the necessary holes for servo installation and for the camera bolt.


Camera bolt was taken from an unused small tripod so as to ease the camera installation.


Arduino Board is installed onto the box plate.



Arduino, Servo and MPU6050 have been installed on the box.


Video of Servo testing on box



Video on the testing of installed camera



 

Step 4: TIME TO TRY

Picture of TIME TO TRY
gyro16.jpg
Camera is placed on motorcycle.


Let’s ride to try …




Lessons Learned:
• Gyro + accelerometer modules should be installed firmly.
• Calibration of horizontal level should be on flat area
• Use powerful servo.
• Use heavy duty battery so hat power to servo is well supplied.

Good luck.

Saftari

Translated by Taufik Masjhur

This post was originally published on saft7.com in Bahasa Indonesia language.


坤照黃20 days ago

Hi!, Nice to meet you , I am sam.I got
the problem that about Gyrocam for motor
is I am using the MPU6050+Kalman to install into a motor , If I shake the MPU6050 relatively
strong up and down it just messes it slowly drifts the camera to one side or
the other .it still doesn't work properly. Could you kindly tell me how
to figure the problem out ?

Have a nice day !!!!

ThomasB32 months ago

Good idea, but please it's possible to do a video without these music, because in Germany the censorship block your video. Its sad, I know.

StraydogEOD10 months ago
First of all, thanks for the awesome write up. I'm trying to compile this code in a sketch and I get the following errors:




sketch_dec22a.ino:25:20: error: Kalman.h: No such file or directory
sketch_dec22a:26: error: 'Kalman' does not name a type
sketch_dec22a:27: error: 'Kalman' does not name a type
sketch_dec22a.ino: In function 'void setup()':
sketch_dec22a:71: error: 'kalmanX' was not declared in this scope
sketch_dec22a:72: error: 'kalmanY' was not declared in this scope
sketch_dec22a.ino: In function 'void loop()':
sketch_dec22a:99: error: 'kalmanX' was not declared in this scope

Do you happen to have any guidance on how to fix them? Thanks!
sartikahusain11 months ago
keren, bisa di kembangkan nggak ya supaya bisa jadi tugas akhir saya ? hehe trims mohon reply nya yaa
faziefazie12 months ago
Keren mas :)
liorslak1 year ago
hey,
nice project you have.
i tried to built it in arduino but it did not work for me.
i have download a kalman.h from the internet. i am using an arduino duemilanove.
thanks.
Very cool, except that's not a motorcycle - it's a scooter...
ant...1 year ago
Ive been working on a motorcycle gyro cam and am failing, my current servo resets itself after 1 second, as my bike can be cornering for over 3 seconds, this type of servo is useless.

Findings so far:
- Most accelerometer based systems ive seen or used are too jerky and ive not yet found decent results from RC Gyro Servo's, but im no expert. Ive been told to use more sensors and average the readings but I wouldnt know how.
- MotoGP use GPS to find the angle of the bike, GPS bank angle sensors are common in motorcycle racing, but I dont currently know how to make a servo alter the angle of the lens in conjunction with the bank angle sensor data? I believe this is the answer.


As im no expert with audrino's im currently working on an old fashioned gyro to stabilise my camera. Basically its a motor spinning a heavy steel wheel.
Just a thought, you could try keeping track of where you last sent the servo, and then when you loop through the next time give yourself a buffer (if new position is +- a certain amount from the previous amount ignore it). That might get rid of shaking from small bumps. The 'random' movement when it turns on sounds like a problem with other sensors I've seen where you need to give them a set amount of time after powering up so things can settle and then you can read it. Maybe just a delay in your setup could fix that. Another idea I had for smoothing is to keep a list of the past five readings or so and check to make sure it isn't going out and then back at any point, and then move to the correct spot. In some of my projects I found that the logic itself was fine, but it was processing too quickly to ever let the hardware catch up. Hope this was helpful, I may try to make this using a wii motion plus as my gyro.
Cibico991 year ago
Some smoothing in the code could be done to have the arduino make better and smoother adjustments. Quadcopters do this, maybe find out if they just do an average or how it is calculated.
saftari (author)  Cibico991 year ago
I'm looking the code to make the servo move smoothly, have you a code or calculation to solve this?
your servos you would be able to fit in this application simply do not have the resolution necessary with the default arduino servo library, you may be able to leverage more precision out of the servos by manually altering the waveform through the code, good luck
saftari (author)  kyle brinkerhoff1 year ago
Hi Kyle,
Can you share the link that give a sample code to make a servo move smoothly?
thanks!
so heres how to do it , look up the standard 90 degree position waveform of the pwm signal, then look up the absolute max and mins of that waveform so that you have some fixed points to judge against, then it only becomes a matter of changing the distance from one crest of the waveform to the next crest. wich can be achieved through the use of a loop that has variable timing capabilitys, that will then in turn give you the absolute maximum precision possible, also look into how your servos operate, some servos really can have what i call a one-to-one replication of angle to value, others will operate on whatever waveform you throw at them,
i think it's because of slow servo-motor
saftari (author) 1 year ago
I need a sample code to make a servo move smoothly without jitter, and how to eliminate servo 'random' movement when the unit just start up/turn on.
Need sharing and info. Thanks a lot!
Why not adapting this to work together with a hand-held steadycam rig to dampen the boom's movements? Instead of levelling the camera with a servo, you could use the servo to adjust the small errors that remain after the mechanical (pendulum) dampening, like an inverted pendulum (Segway). Great instructable!
saftari (author)  stringstretcher1 year ago
Thanks bro!.. waiting for your project result.
LeroyR1 year ago
Very nice instructable! I want to make one myself. I'm not familiar with the arduino boards, so I'd like to ask you, is there a reason you have to use a mini braidboard instead to connect everything direct to the arduino uno?
saftari (author)  LeroyR1 year ago
Thanks,
You can use universal PCB to put additional components, then connect to arduino board
hmmmm... gyro sensors people can afford to use with the arduino tend to have a rather slow response, the reason for this is that inertial forces can actually throw the return signal off of the true angle value, this is due in part to their method of operation, using a small void that has a pocket of gas that moves about on a grid of conductivity sensors, so that pocket of gas is subject to the same forces, but this is the reason why in actual military applications of gyroscopes , for instance a gyro stabilized gun mount for a helicopter side gunner, dont use servos and sensors to achieve the task, they simply do not yet have the finnesse to do the job properly, i would love to see this instructable done with a traditional spinning weight gyroscope to counter act the motion, that would be something that could actually be a very viable product to the moto GP community
you wanna sell one of these... i could really use it?
WhiteTech1 year ago
Very cool! Although I just used my KK2 board (Quadcopter control board) I got it for $19.99, has 2 axis camera stabilization built in, All you need to do is hook up power and 2 servos.
Cool. How 'bout a quickie instructable with parts list and assembly?
billgeo djimdy1 year ago
Yeah, how 'bout that?! :-)
Alright! although that means partially disassembling my quadcopter unfortunately
THATS GREAT (the instructable I mean, not the extra work).
You could also do a detailed write up instead of disassembling the thing...
Yessss! ;-D
saftari (author)  WhiteTech1 year ago
wow cool... can you share your work?
rpowell2k61 year ago
Good idea, try adding some pots inline with the accelerometer to help dampen the feedback power as well as using a faster servo.
saftari (author)  rpowell2k61 year ago
hmm.. can you show me the diagram?
ElvenChild1 year ago
Theoretically, couldn't you just mount the camera on a rod so that it could freely move left and right, and hang a weight from the camera so the weight makes sure it stays horizontal?
No. With only passive components, you might be able to improve the stability under some conditions, but it would probably oscillate wildly under others.
So what you're saying is that when the bike straitens out, the weight (and therefore the camera) would swing like a pendulum?
Sort of. What really happens is that the dynamic motion of the bike at the attachment point is the input to the dynamic system you will create with your mass and any friction and whatever degrees of freedom the components have. Then when you solve all the equations of motion you can learn by taking http://ocw.mit.edu/courses/mechanical-engineering/2-032-dynamics-fall-2004/index.htm , you will probably find that it is stable under some conditions and unstable at others.

The equations may also tell you that everything is fine as long as your added weight is 20kg or if you put the whole thing in an oil bath like a ship's compass.

Servo control is a lot more likely to work than passive components.
saftari (author)  ElvenChild1 year ago
you're right, so we have to use a lighter camera and use a strong servo. in my first test, I use MicroServo 9g, the servo cannot hold the camera weight (100gr) when the power shut down.
kugelfang1 year ago
Mantap mas bro...:D I'm waiting for your other articles..! Thanks...
saftari (author)  kugelfang1 year ago
Thanks mas bro!...