Skip to content

Arduino MAIN v1

AutoModelCar edited this page Feb 19, 2018 · 3 revisions

Install Ardunio Software:

You can install Ardunio Software from link below:

 https://www.arduino.cc/en/Main/Software

Then Download external libraries and codes for the model car from link below:

 git clone https://github.com/AutoModelCar/auto_arduino_nano.git

Copy the (MPU6050, MsTimer2, I2Cdev) libraries to the arduino-1.6.9/libraries folder.

version 1.6.9

hints:

  • Turn off the car.
  • Connect the USB port to Arduino nano.
  • From toolbar: Tools select Arduino nano and select the correct USB port (e.g :/dev/ttyUSB0)
  • Add permission to use the USB Port: sudo chmod 777 /dev/ttyUSB0
  • Compile and upload the code.

Main.ino

Main code allows the Arduino board to:

  • Control the status light LED,
  • Check the battery voltage,
  • Turn on/off the car’s lights,
  • Control the steering servo motor,
  • Calculate the yaw angle from MPU6050 through I2C.

Control the status LED

The green Button is connected to Digital Pin 4 while the red button is connected to Digital Pin 5 of the Arduino board. When the buttons are pressed, you can read a 0 through the digital pins. In order to provide 5 Volts to the mainboard, Digital Pin 8 and Digital Pin 9 should be LOW. In order to provide 15 Volts to the Faulhaber motor, Digital Pin 8 should be HIGH and Digital Pin 9 should be LOW. To turn off the mainboard, Digital Pin 8 should be LOW and Digital Pin 9 should be HIGH.

Check the battery voltage

Allows the Arduino board to check the voltage of the battery through ADC 7. If the battery voltage is under 14.8 voltage, the status light will blink. If the battery voltage is under 13 V the car will be shut down, and the status light will become red. The battery should be recharged. The diagram below shows the battery voltage vs. time, with all components running.

Figure 15: Battery voltage vs. time while the car drives with negligible friction.

Turn on/off the car’s lights

Allows the Arduino board to turn on-off the lights like a real car. For example, you can turn on the left lights while the car is turning left. In table below you can see which pin is connected to which lights.

 Digital Pin    Lights  
 D11            Brake lights 
 D13            tail light, Parking light 
 A0             turn left signal 
 A1             turn right signal 
 A2             reversing light 
 A3             headlight

Table 1: Pins of Arduino which are connected to the lights

Control of the servo motor

Allows the Arduino board to control the servo motor. The Arduino will read the desired steering angle through the serial port from Odroid and it will set the appropriate Pulse Width Modulated (PWM) signal for the servo motor. A pulse is sent every 20 milliseconds. A servo motor is controlled by sending a PWM signal through the control wire. The width of the pulses determines the position of the shaft. We assume that a pulse of 0.9ms will move the shaft anticlockwise at -90°, a pulse of 1.45ms will move the shaft at the neutral position that 0° and a pulse of 1.9ms will move the shaft clockwise at +90°. You maybe need to calibrate the servo motor again by changing the line below in the main code. val = map(val, 0, 180, 900, 1900); Be very careful not to define an angle that pushes the servomotor continually against the mechanical limits of the steering wheel. This could burn the motor, if the motor presses for too long against the mechanical limits.

Figure 16: PWM signal for controlling the shaft of the servomotor

Calculate the yaw angle from MPU6050 through I2C

Allows the Arduino Board to initialize the I2C device and read the raw data (gyroscopes and accelerometers) from the MPU6050 and convert it to the yaw angle. Then it sends the yaw angle in degrees through the serial port to the Odroid. To read the yaw angle, you should wait at least 10 seconds at the beginning -- then you can read the correct yaw angle in degrees with 115200 baud rate .

Serial.print("ypr\t");//yaw
Serial.println(ypr[0] * 180/M_PI);

Figure 17: Yaw angle in relation to the car’s body

Calibrate the IMU for correct yaw angle

When your yaw angle continuously drifts while the car is not moving or the angles doesn't match with the real world, then you should calibrate the MPU6050 sensor again.

Turn on the lidar and use the IMU_Zero/IMU_Zero.ino code to calibrate the IMU again. It takes about 10 minutes. You should update the lines below in the main.ino code, and test it again.

    mpu.setXAccelOffset(-1643);
    mpu.setYAccelOffset(589);
    mpu.setZAccelOffset(1333);
    mpu.setXGyroOffset(418);
    mpu.setYGyroOffset(-57);
    mpu.setZGyroOffset(69);

Clone this wiki locally