CE432 2022 Fall
Lab 9
Name: Braden Morrow Email: bdmorrow@fortlewis.edu
1. Lab 9 - Design a Two-Wheel Balancing Car
2. Introduction The purpose of this
tutorial was to control a Two-Wheel Balancing Car kit using a joystick.
Most of the code is provided by Elegoo (the manufacturer of the kit we
are using), however we worked on optimizing this code and making it
compatible with our joystick and 2.4 GHz transievers.
3. Results Week 1:
This week I decided to start slow by assembling the car and
trying to understand/optimize the code provided by Dr. Li which
provides the base functionality of the Tumbller.
The I2C implementation Elegoo provided is bloated because they
just took the entire libraries for both I2C and the MPU6050 and added them to the project. This is way
more than we need, so I wrote a simple function which pulls the
accelerometer data. This file can be
found in Tutorial 9 -> Tumbller -> MPU.cpp.
Figure 1 shows the basic
balancing routine running after some debugging to find the right target
angle for my Tumbller, using the optimized I2C routine.
Figure 1. Tumbller Basic Balancing Routine
The
function Dr. Li provided to balance the Tumbller conventiently accounts
for a car speed and rotational speed. I added some mode functionality
and now the Tumbller can either idle or go through some basic motions
(forward, backward, turn left, turn right). This was also a convenient
time to stop the Tumbller from trying to balance when it was over
rotated.
Figure 2. Tumbller Basic Movement Routine
Next week I plan to integrate the joystick and 2.4GHz transievers into the mode functionality.
Week 2:
This week I replaced the default "MSTimer" scripts with a basic
timer based off of the Arduino's internal clock. This implementation
was significantly shorter than theirs and works just as well. I used
Timer1 to set the 5ms loop for the balance routine, and Timer2 to set a
100ms routine for getting the joystick input from the 2.4GHz
transciever. I found that without an interrupt for the transciever the
output would constantly get set to 0 while the balance routine was
running.
In order to get the transciever
working with the pinout of the Tumbller (which only has 5 pins not
affected by other circuitry in the the bot) I had to implement a
softSPI connection. The library for the transciever had a softSPI function built in so I just had to specify the pins that could be used by the transciever. Figure 3 shows the final product, a Tumbller which can be controlled by a joystick connected to a second arduino.
Figure 3. Tumbller Joystick Control
Hardware Connections: For this project, the only part we had to wire to the Tumbller was the 2.4GHz transciever. Figure 4 shows those connections.
Figure 4. 2.4GHz Transciever Pinout
Code: The changes to the code I
made were mostly cleaning up their code and making it much smaller.
Figure 5 shows the I2C code for the MPU6050 which replaced thousands of
lines of code.
Figure 5. I2C for MPU6050
Next, I found some basic mode functionality to control the movement of the robot from Elegoo's origninal tutorial. Figure 6 shows the associated speeds for each of the movement modes.
Figure 6. Motion Mode Fucntionality
I then added the
timers to control when we pulled the joystick input and ran the balance
routine. The balanace routine is on a 5ms timer and the joystick input
is on the 100ms input.
Figure 7. Timers for Balance and Radio Routine
Finally, here is the code used to get data from the 2.4 GHz Transciever.
Figure 8. 2.4GHz Transciever Code
Design Description: The only thing worth noting
here is that SoftSPI needs to be implemented on the transciever to
enable it to use the new pins. These two changes to the library for the
transciever turns on their built-in SoftSPI functionality.
Figure 9. 2.4GHz Transciever Library Changes for SoftSPI
5. Discussion This
project, while seeming straightforward at first, took the most time out
of any project I've worked this semester. Essentially this just came
down to chasing down bugs caused by my code interacting with some part
of their code I was unaware of. One issue I found with this kit is that
their "pinout" doesn't include any extra pins not directly used in the
example functions they provided. In more than one case, this meant most
of the pins were useless for getting any kind of input as they were
already being used by components integrated into their PCB. However,,
the Tumbller was much easier to work with than a custom made bot and
provided a lot of useful script examples for controlling the bot.