Prelab
In prior labs, my batteries had been placed in the battery compartment of the car without anything securing them other than the wires that connected them. However, in this lab, the car will be preforming high speed stunts which means that every component must be properly secured. Therefore, I added an extra rubber band to ensure that the batteries would not fall out of the car incase it crashed into a wall or flipped over.
The car is now ready to preform stunts!
Drifting Steps
The drift stunt consists of three different steps.
- Approach: The car must quickly approach to 3ft from the wall.
- Drift: A quick 180 degree turn to orient the car towards its start direction.
- Return: The car returns as quickly as possible to the start line.
Therefore, I created a state variable called mode which will allow the car to keep track of which step it is currently on in the stunt.
- mode = 0: Approach
- mode = 1: Drift
- mode = 2: Return
Drift Controller
I then created a function called drift controller which handles each portion of the stunt.
Approach
The approach step is handled by the following code snippet which essentially drives the car forward until the car is within 1214mm of the wall. Note that this is larger that the actual 914mm in the requirements because my TOF sensor is angled slightly upward and would therefore measure a longer distance than the actual distance to the wall.
Drift
Then, once the car is close enough to the wall, we can initiate a 180 degree turn using the orientation controller implemented previously. Note, I added return statements to the orientation controller so that it would return 1 once the error is within the threshold and zero if it is not.
I also created a helper function compute_oposite which computes the reference angle 180 degrees from the current orientation.
Return
The final part of the controller drives the car forward at full power for 0.5s. This allows the car to return to the start line.
Main Loop
Here is the controller implemented in the main loop of the code very similar to how the previous controllers were implemented.
Plots and Video
Here are plots and video of the the car drifting. As shown by the distance plot, the car approaches the wall and then spins around. As the car spins, the distance sensor becomes very noisy and large as it is now measuring an open room with no wall. Another interesting thing to note is that the orientation controller saturates a lot during the drift leading to excess oscillations. Therefore, it could have probably been tuned better to reduce these ocillations and the time it takes to complete the manuever. Overall, this manuever took 3.02 seconds to preform.
Here is the accociated video of the stunt.
Additional Trials
Here are additional trials of the car preforming the stunt.
Conclusion
Overall, I had a lot of fun during this lab. I really got to put together the pieces from pervious labs to create something cool!