In this activity, you will add cyber:bot navigation maneuvers to Gripper motion. This will make the cyber:bot robot roll to, pick up, move, and set down an object.
(1) cyber:bot with Gripper installed, tested and tuned by following the Parts, Circuit, Script and Tests sections in the previous Test and Tune the Gripper [1] activity.
This script makes the Gripper-equipped cyber:bot:
Let's give it a try.
cyber_bot_gripper_forward_object.hex [2]
The script was created by entering the text below into the python.microbit.org/v/2 editor. The cyberbot.py module was also added to the Project Files as shown in Add modules to your micro:bit [4]. The Script Name was set to cyber_bot_gripper_forward_object before saving it as a .hex file as shown in How to Save and Reopen a Script in Python Editor v2 [5].
# cyber_bot_gripper_forward_object from cyberbot import * display.show(Image.ARROW_E) while True: if button_b.was_pressed(): display.clear() bot(16).servo_angle(150) # lower & open gripper sleep(1500) bot(18).servo_speed(130) # Forward 0.5 s bot(19).servo_speed(-130) sleep(500) bot(18).servo_speed(None) bot(19).servo_speed(None) sleep(1000) bot(16).servo_angle(30) # close gripper & lift sleep(1500) bot(18).servo_speed(130) # Forward 1.25 s bot(19).servo_speed(-130) sleep(1250) bot(18).servo_speed(None) bot(19).servo_speed(None) sleep(1000) bot(16).servo_angle(150) # lower & open gripper sleep(1500) bot(18).servo_speed(-130) # Backward 0.5 s bot(19).servo_speed(130) sleep(500) bot(18).servo_speed(None) bot(19).servo_speed(None) display.show(Image.ARROW_E)
Your cyber:bot robot might curve to the left or the right during these maneuvers. If it does, it can be straightened by slowing the servo that’s going faster and causing the curve. A remedy for this is included in the step-by-step instructions below.
After importing the cyberbot module, the script goes into an endless while True: loop that monitors the tilt micro:bit module’s B button.
# cyber_bot_gripper_forward_object from cyberbot import * display.show(Image.ARROW_E) while True:
If the B button was pressed, the cyber:bot starts by rolling forward for ½ a second, then stops. The routine is appended with a sleep(1000) call to make sure the cyber:bot has completely come to rest. Once at full stop, the script uses bot(16).servo_angle(30) to close the Gripper. It is followed by sleep(1500) to give the Gripper paddles time to close and lift, and partially to make a slight pause between picking up the object and starting to move.
if button_b.was_pressed(): display.clear() bot(16).servo_angle(150) # lower & open gripper sleep(1500) bot(18).servo_speed(130) # Forward 0.5 s bot(19).servo_speed(-130) sleep(500) bot(18).servo_speed(None) bot(19).servo_speed(None) sleep(1000) bot(16).servo_angle(30) # close gripper & lift sleep(1500)
After lifting the object, this part of the script makes the cyber:bot roll forward for 1.25 s. Then, it lowers and opens the Gripper paddles, setting down the object.
bot(18).servo_speed(130) # Forward 1.25 s bot(19).servo_speed(-130) sleep(1250) bot(18).servo_speed(None) bot(19).servo_speed(None) sleep(1000) bot(16).servo_angle(150) # lower & open gripper sleep(1500)
Finally, it backs up for 0.5 s so that the object is no longer between the Gripper paddles.
bot(18).servo_speed(-130) # Backward 0.5 s bot(19).servo_speed(130) sleep(500) bot(18).servo_speed(None) bot(19).servo_speed(None) display.show(Image.ARROW_E)
It is often a good idea to put things back where you got them when you are done with them! Let's add a Button A feature to make the cyber:bot take the object and place it back where it started before you pressed Button B.
Your Gripper-equipped cyber:bot is not limited to forward and backward motion! With some modifications to the script, you can add any of the turns introduced in the Navigation with the cyber:bot [6] chapter’s Left and Right Turns [7] activity.
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/gripper-cyberbot-robot/test-and-tune-gripper
[2] https://learn.parallax.com/sites/default/files/content/cyberbot/Gripper/Hex/cyber_bot_gripper_forward_object.hex
[3] https://python.microbit.org/v/2
[4] https://learn.parallax.com/tutorials/robot/cyberbot/add-modules-your-microbit
[5] https://learn.parallax.com/tutorials/robot/cyberbot/how-save-and-reopen-script-python-editor-v2
[6] https://learn.parallax.com/tutorials/robot/cyberbot/navigation-cyberbot
[7] https://learn.parallax.com/tutorials/robot/cyberbot/navigation-cyberbot/left-and-right-turns