The purpose of this exercise is to write software that uses sensor data to maneuver the Pololu robot. You will gain experience with additional sensors.
For this lab, we will be detecting and maneuvering around obstacles using the Pololu robot. What sensor(s) on the robot will be most useful for detecting obstacles?
For this lab, we will be using a gyroscope to sense turns made by the robot. The ST LMS6DSO inertial module, which you encountered in the Sensors lab, includes a gyroscope as well as the accelerometer you used in that lab. The gyroscope senses angular velocity and reports it in units of degrees per second (o/s).
There is a Lingua Franca reactor called
Gyroavailable to you in
src/lib/IMU.lfthat outputs gyroscope measurements in units of degrees per second when triggered. Suppose that what you want is not angular velocity but rather a measurement of the current angle of the device relative to some starting angle. Explain how you can convert an angular velocity measurement into an angle measurement. Specifically, given the i th measurement vi of an angular velocity that is taken at time ti, how can you construct an estimate of the angle ai at time ti?
The speed of motors can be controlled using a technique called pulse width modulation (PWM). The RP2040 chip includes hardware for controlling up to 16 motors using PWM, as explained in Section 4.5 of the RP2040 datasheet. For this purpose, each PWM hardware block can be connected to a GPIO pin, and then you can use the GPIO pin to control the motors. Which GPIO pins are used to control the motors on the Pololu robot?
Hint: The Pololu 3pi+ 2040 Robot User's Guide might be helpful.
Note: The GPIO pins cannot drive a motor directly because they cannot source enough power without damaging the chip. The Pololu robot includes a Texas Instruments DRV8838 motor driver which takes as input the PWM signal and provides power to the motors.
Using the notation from the textbook for state machines, sketch a state machine for a robot controller that makes the robot move in a square. That is, when the program is run, the robot should move forward some distance D, turn 90 degrees, move forward a distance D again, and repeat these actions.
Your first task is to drive the motors on the Pololu robot. For your convenience, a library reactor called
Motors is provided in
src/lib/Motors.lf. You should start with the provided LF program
src/RobotTemplate.lf. Start by verifying that this template works for you:
lfc src/RobotTemplate.lf picotool load -x bin/RobotTemplate.elf
The robot should start by displaying
INIT, then switch between
Motorsreactor. How does it work? Find the
motors_set_powerfunction that it uses. (Hint: the search function in VS Code is very useful for this.) Explain how the implementation of this function conforms with the answer you gave in question (3) of the prelab.
Motorsreactor to make robot move forward while it is the
DRIVINGmode and stops while in the
STOPPEDmode. You can experiment with the power to provide, but a good starting point is 0.1f. Please put your solution in a file called
Hint: The motors will not run if the only source of power to the robot is the USB cable. To get the motors to run, you must insert batteries and push the power button so that the blue power indicator LED is illuminated.
Checkoff: Show that you understand the implementation of the
motors_set_powerfunction and that your robot moves forward and stops.
Encoders measure wheel rotation and can be used to estimate the distance traveled. A technique known as dead reckoning uses such measurements to help to know where the robot is located relative to some starting point. Our goal here is to create a reactor that takes as input an encoder reading and outputs an estimate of the distance traveled since the program has started for each wheel.
The output from the encoders is in degrees given as a 32-bit integer.
Examine and run the
src/EncoderDisplay.lf LF program.
You will convert these numbers to distance.
The diameter of the wheels on the robot is approximately 3.175 cm. Find a formula that converts a change in angle in degrees to distance traveled in meters.
The encoder outputs increase as the wheels rotate forward. Given that the values are 32-bit signed integers, how far does a wheel need to travel before the numbers will overflow? Do you think you need to worry about overflow for these labs?
Write a reactor
AngleToDistanceto convert a change in encoder value to distance. Then create a variant of
src/EncoderDisplay.lfthat displays distance traveled for each wheel rather than angle in degrees. Please put your solution in a file called
Checkoff: Show that your distance measurement is reasonably accurate.
As you (hopefully) determined in problem (2) of the prelab, the gyroscope output can be integrated to get a measure of the current angle of the robot relative to some starting point.
You are provided with a reactor
src/lib/IMU.lf that uses the trapezoidal method to calculate the angle. Use this reactor to create modal Lingua Franca program
RobotSquareSolution.lf that drives for approximately half a meter, turns 90 degrees, drives another half meter, and then repeats, so that the robot moves roughly in a square. What factors contribute to the imperfection of the square?
Checkoff: Show your robot moving in a square and show the diagram of the modal Lingua Franca program.
Examine the src/BumpDisplay.lf example program provided by your template repository. How does it work? What does the Bump reactor class do?
Your task now is to use the Bump reactor class to modify your previous solution so that when the robot bumps into an obstacle as it navigates around the square, it backs off in such a way as to avoid the obstacle. Please put your solution in a file called
Checkoff: Show your robot evading obstacles. Show your modal model.
If the bump sensors fail to detect an obstacle that has stopped your robot, the robot will stop making progress because it will fail to cover the requisite distance to switch to the next mode. How might you change your program to ensure that the robot continues to make progress?
Instead of setting the power level of the motors, a better strategy might be to set a desired robot speed. How might you use the encoders to measure the speed? How might you use the speed measurement to control the speed? Hint: Section 2.4, Feedback Control, of Lee and Seshia might be useful.
What were your takeaways from the lab? What did you learn during the lab? Did any results in the lab surprise you?