Skip to content

Arduino MAIN v3.1 rosserial

AutoModelCar edited this page May 18, 2018 · 2 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
 cd auto_arduino_nano
 git checkout version-3-rosserial

Copy the (MPU6050, Adafruit_NeoPixel, I2Cdev, Rosserial_Arduino_Library) libraries to the arduino-1.8.5/libraries folder.

version 1.8.5

hints:

  • Turn on 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(D6),
  • Control the steering servo motor(D10),
  • Read the feedback from servo motor (ADC0)
  • Calculate the yaw angle from MPU6050 through I2C(A4/5 and D2).
  • Control the speed of the brushless motor (D11 and D4)
  • Read the feedback from the brushless motor(D3 and D4)
  • Communication with Arduino using Rosserial library

Control the status light LED(D6)

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. Pin D6 is connected to LEDs.

Control the steering servo motor(D10)

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 10 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 1.0ms will move the shaft clockwise at -25°, a pulse of 1.5ms will move the shaft at the neutral position that 0° and a pulse of 2.0ms will move the shaft anticlockwise at +25°. You maybe need to calibrate the servo motor again by changing the line below in the main code. val = map(val, 0, 180, 1000, 2000); 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

Read the feedback from servo motor (ADC0)

The servo motor feedback is connected to ADC0. Rosserial publishes it every 10 milliseconds on /steering_angle Topic through serial port. By subscribing to /steering_angle topic, the feedback on the Odroid can be read.

  rostopic echo /steering_angle

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 100Hz .

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

Control the speed of the brushless motor

Pin D11 provides PWM signal for brushless motor. [OC2-8 bit pwm, frequency:7812 Hz]

Pin D4 controls the direction of the motor.

Read the encoder and calculate the revolutions and speed.

Pin D3 reads the encoder pin of the motor. (timer2-frequency:7812 Hz)

Communication with Arduino using Rosserial

The arduino uses the rosserial library to communicate with ROS. ros-kinetic-rosserial provides a node which listens to the Serial port offers topics to subscribe and publish from and to the arduino. Make sure you have this package installed. If you just want to setup ROS communication with the Arduino you can use the provided launch file: roslaunch manual_control rosserial.launch

IMU_Zero.ino

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