The primary goal of my final project is to implement the ultrasonic sensor on the robot car and also use the compass and MPU-9250 registers. Furthermore, I am also going to use the gyro registers data from the robot car to output the current position (x, y coordinates) of the robot car on the floor.
For the final presentation, I expect my robot car will work to avoid obstacles and the teraterm will print the current x and y coordinates of the robot car. When the ultrasonic sensor detects the obstacle 30cm away, the robot car will turn left and play the certain sequence of sound from the buzzer to notify the turn. While the robot car is moving, the tera term will output the x, and y positions of the robot car based on the gyro output from the MU-9250 registers and also the theta calculated using formula shown below.
Demo VideoThe demo video is avaialbe at:
Videos Explaining Parts used and How to connect to the launch padThe explaination of the parts used in the project and the connection of the parts to the board:
Explaining the ProjectFor this project, two most important parts are the application of the ultrasonic sensor and the control of the motors. First, to properly implement the ultrasonic sensor, we first need to connect it to the launch pad we use which is the F28379D launch pad. The working voltage of the ultrasonic sensor is 5 V. We can connect to the 5 V source pin on the launch pad or connect to any 5 V source on the green board. In this project, I soldered the power supply from the green board to the ultrasonic sensor. The ultrasonic sensor has four pins which are the vcc, trigger pin, echo pin, and the ground pin. We can connect the trigger pin and the echo to any GPIO registers of the F28379D launch pad as long as the choice does not interfere other sensors and chips. In this project, the trigger pin is connected to GPIO 15 and the echo pin is connected to GPIO 14. Based on the data sheet of the ultrasonic sensor, the trigger pin needs to be low for at least 2ms and then change to be high for 10ms for ultrasonic sensor to work. Based on my testing, setting the initial low state time longer might help the ultrasonic senor to precisely detect longer distance. In this project, the trigger pin is commanded to stay low for 3ms and then high for 10 ms. Finally, I used the ecap peripheral of the F28379D launch pad to capture the time spent by the signal to be received by the echo pin. The echo distance is calculated based on the echo duration and the velocity which is 340 m/s. The code snippet below shows the method to trigger the ultrasonic sensor, We store the state of the ultrasonic sensor in the parameter trigger_state, when trigger state is 0, we set the parameter to be 1 which is low state for 3ms. When trigger count is greater than 3 ms, we set the trigger_state to high which is 2 and last for 10 ms.
void hc_sr04_trigger(void) {
if (trigger_count < 3) {
if (trigger_state == 0) {
// last for 3ms
GpioDataRegs.GPACLEAR.bit.GPIO15 = 1;
trigger_state = 1;
}
trigger_count++;
} else if ( (trigger_count >= 3) && (trigger_count < 12) ) {
// last for 10ms
if (trigger_state == 1) {
GpioDataRegs.GPASET.bit.GPIO15 = 1;
trigger_state = 2;
}
trigger_count++;
} else {
if (trigger_state == 2) {
trigger_count = 0;
trigger_state = 0;
}
}
}To calculate the x and y coordinates of the robot car, I applied the equations from the paper written by Yorihisa Yamamoto from the "NXTway-GS Model-Based Design - Control of self-balancing two-wheeled robot built with LEGO Mindstorms NXT". This project can be found using the link below:
Specifically, the equations used for calculating x and y coordinates are equations 3.1 and 3.2 from the paper.
The velocity in the x direction and in the y direction are calculated by using the radius of the wheel times the angular velocity of the wheel, and then times the angle change of the motor. After computing the velocities in x and y, we can directly integrate the velocity based on the timesteps we set to get the x and y coordinates. In this project, the x and y coordinates can be viewed by using teraterm. The code below is the implementation of this formula and calculation.
LeftWheel = readEncLeft();
RightWheel = -readEncRight();
VLeft = (LeftWheel - LeftWheeli)/0.004;
VRight = (RightWheel - RightWheeli)/0.004;
dtheta = (VLeft + VRight)*0.5;
thetaabs = (0.108/0.679)*(RightWheel - LeftWheel);
x_dot = (0.108*dtheta)*cos(thetaabs);
y_dot = (0.108*dtheta)*sin(thetaabs);
x_pos = x_posi + (0.004*((x_dot + x_doti)/2));
y_pos = y_posi + (0.004*((y_dot + y_doti)/2));The x_posi, y_posi, x_doti and y_doti are parameters of the past states (last time step), and we used these parameters for the time integration.
The gyro readings from the MPU 9250 registers can also be used to compute the change of motor angle. The formula used is to integrate the gyro readings based on the time steps which is 0.004s in this project.
The buzzer is connected to GPIO 16 of the F28379D launch pad, and EPWM 9A is applied to control the buzzer. When the buzzer is needed, we need to set the pinmux to 5 which corresponds to the EPWM 9A. When we do not need to use the buzzer, we can set the pinmux to 0 to stop it from make noise. In this project, buzzer is used as the signal to notify the obstacles. Whenever something is detected by the ultrasonic sensor, the buzzer will make the sound to notify it.
For the MPU 9250 registers, the SPI (serial peripheral interface) is used to send and receive the bits and data needed from the MPU 9250 registers. We set up the SPI to receive the data from accelerometer and the gyro readings from the MPU 9250 registers. The MPU 9250 is connected to GPIO 63, GPIO 64, GPIO 65, and GPIO 66 of the F28379D launch pad.
Basic Logic of the ProjectThe logic of the robot car in this project is that in cpu timer 0, the registers related to MPU 9250 are transmitted and the ultrasonic sensor is triggered in cpu timer 0. In cpu timer 1, the parameters related to motors are calculated. We used the if statement to decide whether to turn. If the echo distance returned from the ultrasonic sensor is smaller than 30cm, the turn parameter will increase to 1. The car will start turning to the left. The pinmux of GPIO 16 which connects to buzzer will set to channel 5 which is EPWM 9A for the buzzer to make the sound. When ultrasonic sensor does not detect anything, the pinmux of GPIO 16 is 0.
ConclusionBased on the testing and observation, the robot car can work properly with the implementation of the ultrasonic sensor. Once the robot detects the obstacle, it will turn to the left and the buzzer will make the notify sound at the same time. The teraterm will output the orientation and the x and y coordinates of the robot car.
The circuit schematic and the board are designed by Professor Block (Dan Block) from University of Illinois at Urbana Champaign. Email: d-block@illinois.edu









Comments