Introduction: Tuning the GiggleBot Line Follower - Advanced
In this very short Instructables you are going to tune your own GiggleBot to follow a black line. In this other tutorial GiggleBot Line Follower, we hard-coded the tuning values to work according to that scenario. You might want to make it behave better by coming up with other gains.
In this tutorial, we are showing you 2 scripts that can both be loaded on different BBC micro:bits so that one of them is put into the GiggleBot and with the other one, the 2 buttons are used to go through a menu and tune different parameters. The sending of these updated parameters is done via the radio.
Step 1: Required Components
You will need the following:
- A GiggleBot robot for the micro:bit.
- x3 AA Batteries
- x2 BBC micro:bits - one for the GiggleBot and the other one acting as a remote for tuning parameters.
- A battery for a BBC micro:bit - like the one that comes within the BBC micro:bit package.
Get the GiggleBot Robot for the BBC micro:bit here.
Step 2: Setting Up the Tracks & Environment
You also have to actually build your tracks (download, print, cut and tape tiles) and then set up the environment (the IDE and the runtime).
Since this tutorial is very related to this other tutorial entitled GiggleBot Line Follower, just go there and follow steps 2 and 3 and then come back here.
As for the IDE, you can use the Mu editor and for the runtime, you need to download the GiggleBot MicroPython Runtime. The runtime can be downloaded from its documentation here. Head over to the Getting Started chapter of documentation and follow those instructions on setting up the environment. As of this moment, version v0.4.0 of the runtime is used.
Step 3: Setting Up the GiggleBot
Before flashing the runtime to the GiggleBot, make sure you have chosen your desired speed and update rate for the GiggleBot: by default, the speed is set to 100 (base_speed variable) and update rate is set to 70 (update_rate variable).
Given the current implementation, the highest update rate that can be achieved is 70 and if run_neopixels is set to True, then only 50 is achievable. So in a way, you could say that the default update rate is right on the edge of what the BBC micro:bit can do.
Just for the record, the line follower sensor can return updates 100 times a second.
Note: The following script might have missing whitespaces and this seems to be due to some issue in displaying GitHub Gists. Click on the gist to take you to its GitHub page where you can copy-paste the code.
Step 4: Setting Up the Tuner (Remote)
The next thing we have to do is flash the runtime + script to the 2nd BBC micro:bit. This second micro:bit will act as a remote to the GiggleBot, which will be used to tune the following parameters:
- Kp = proportional gain for the PID controller.
- Ki = integral gain for the PID controller.
- Kd = derivative gain for the PID controller.
- trigger_point = the point expressed in percentages between the minimum and maximum speeds of the GiggleBot where the speed starts getting reduced linearly until it reaches the minimum speed.
- min_speed_percent = the minimum speed expressed in percentage of the maximum speed.
The other 2 remaining variables that can be tuned are directly hard coded in the script that sits on the GiggleBot: the update_rate and base_speed which represents the maximum speed. As described in the documentation, the maximum speed that can be set for the GiggleBot is 100, which is also the default value for our GiggleBot.
Note: The following script might have missing whitespaces and this seems to be due to some issue in displaying GitHub Gists. Click on the gist to take you to its GitHub page where you can copy-paste the code.
Step 5: Tuning the GiggleBot
Place the GiggleBot on the track, turn it on and let it run. In the meantime, you'll constantly have to put it back on the track and tune the gains/parameters with the other BBC micro:bit that you're holding in your hand.
To start the GiggleBot, press button A on the GiggleBot's BBC micro:bit and to stop it and thus reset its state press on button B.
On the remote BBC micro:bit, pressing button A will take you through every option in its menu and button B increases/decreases the corresponding value. It's like setting the clock on an old car's dashboard. The options are like this:
- 0-1 options are for the Kp gain.
- 2-3 options are for the Ki gain.
- 4-5 options are for the Kd gain.
- 6-7 options are for setting the setpoint for the moment when the motors start slowing down.
- 8-9 options are for setting the minimum speed.
Keep in mind that even numbers in the menu are for increasing the corresponding values and for the odd ones it is exactly the opposite.
Also, when pressing button B on the GiggleBot's BBC micro:bit, you will see on its Neopixel-made screen the number of elapsed milliseconds since the last reset and the number of cycles that the robot has gone through - with these 2 you can calculate the update rate of the robot.
Lastly and most importantly, I have come up with 2 tunings for the GiggleBot. One of them is for when the Neopixel LEDs are turned off and the other is for when it's otherwise. The Neopixel LEDs are used to show to which direction the error has accumulated.
1st set of tuning the parameters (with NeoPixel LEDs off)
- Kp = 32.0
- Ki = 0.5
- Kd = 80.0
- trigger_setpoint = 0.3 (which is 30%)
- min_speed_percent = 0.2 (which is 20%)
- base_speed = 100 (aka maximum speed)
- update_rate = 70 (running @70Hz)
2nd set of tuning the parameters (with the NeoPixel LEDs on)
- Kp = 25.0
- Ki = 0.5
- Kd = 35.0
- trigger_setpoint = 0.3 (which is 30%)
- min_speed_percent = 0.3 (which is 30%)
- base_speed = 70 (aka maximum speed)
- update_rate = 50 (running @50Hz)
- Also, variable run_neopixels has to be set to True in the script that gets loaded on the GiggleBot's BBC micro:bit. This will make the NeoPixel LEDs flash in such a way that they indicate towards which direction the error gets accumulated.
Step 6: GiggleBot Running With the NeoPixels Turned Off
This is an example of running the GiggleBot with the 1st tuning parameters found in the previous step. This example has the NeoPixel LEDs turned off.
Step 7: GiggleBot Running With the Neopixels Turned On
This is an example of running the GiggleBot with the 2nd set of tuning parameters found in step 5. This example has the NeoPixel LEDs turned on.
Notice how in this example, the GiggleBot has a harder time following the line - that's because the Neopixel LEDs are "eating" the CPU time of the BBC micro:bit. That's why we had to reduce the update rate from 70 down to 50.