This project allows you to use a second micro:bit to tilt control the cyber:bot robot and use its B and A buttons to remotely close-lift and lower-open the Gripper paddles.
For those who have a class set of cyber:bot robots, a second cyber:bot can be used as a tilt controller if an extra micro:bit GO kit is not available on its own.
This is the script to run on the cyber:bot robot. It receives values from the micro:bit tilt controller that are used for its drive servos and Gripper servo.
radio_tilt_grip_controlled_cyberbot.hex [3]
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 [5]. 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 [6].
# radio_tilt_grip_controlled_cyberbot from cyberbot import * import radio radio.on() radio.config(channel=7, queue=1, length=64) while True: packet = radio.receive() if packet: try: dictionary = eval(packet) x = dictionary.get('x') y = dictionary.get('y') needle = dictionary.get('needle') fb = y / 10 lr = x / 10 button = dictionary['button'] # added except Exception as e: display.show(Image.SAD) print("Exception e:", e) print("Type:", type(e)) continue else: if abs(y) > 80: display.show(Image.ALL_CLOCKS[needle]) vL = fb vR = -fb if(fb < 0): lr = -lr vL = vL - lr/2 # Reduced lr term by 2 vR = vR - lr/2 # Reduced lr term by 2 else: display.show(Image.DIAMOND_SMALL) vL = None vR = None if button == 'B': # added bot(16).servo_angle(30) # added Gripper close & lift elif button == 'A': # added bot(16).servo_angle(150) # added Gripper lower & open finally: bot(18).servo_speed(vL) bot(19).servo_speed(vR)
This is the script to run on the micro:bit module serving as the tilt controller for the Gripper-equipped cyber:bot robot. It takes measurements from its onboard tilt sensor and sends radio data values to the cyber:bot for its drive servos and Gripper servo.
radio_tilt_grip_controller [7]
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 [5]. 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 [6].
# radio_tilt_grip_controller from microbit import * import math import radio radio.on() radio.config(channel=7, queue=1, length=64) while True: x = accelerometer.get_x() y = accelerometer.get_y() angle = round( math.degrees( math.atan2(y, x) ) ) needle = ( angle + 90 + 15 ) // 30 if abs(y) > 80: display.show(Image.ALL_CLOCKS[needle]) else: display.show(Image.DIAMOND_SMALL) dictionary = { } dictionary['x'] = x dictionary['y'] = y dictionary['needle'] = needle if button_b.was_pressed(): # add dictionary['button'] = 'B' # add elif button_a.was_pressed(): # add dictionary['button'] = 'A' # add else: dictionary['button'] = 'None' # add packet = str(dictionary) radio.send(packet) sleep(50)
Follow these step-by-step instructions to test the tilt-drive and button Gripper control functions.
The scripts in this project are just slightly expanded versions of the ones from the Cybersecurity: Radio Tilt Control [8] tutorial’s Radio Tilt Controlled cyber:bot [9] activity.
To add button control to the Radio Tilt Controller Code [10], the routine below was added to the transmitter app. It expands the dictionary to contain data indicating which of the micro:bit module’s buttons were pressed. Before this, it just contained x, y, and needle key value pairs.
if button_b.was_pressed(): # add dictionary['button'] = 'B' # add elif button_a.was_pressed(): # add dictionary['button'] = 'A' # add else: dictionary['button'] = 'None' # add
This line was added to the Radio Controlled cyber:bot Code [11].
button = dictionary['button'] # added
These lines were added to respond to the button values by lowering/opening or closing/lifting the Gripper paddles.
if button == 'B': # added bot(16).servo_angle(30) # added Gripper close & lift elif button == 'A': # added bot(16).servo_angle(150) # added Gripper lower & open
The tilt controller might be a little too responsive, but slowing it down isn’t hard. There are two facets to slowing it down.
First, the range where there’s no motion and the micro:bit displays a diamond can be extended from 80 to 200. This will help prevent accidentally moving the cyber:bot when you press a button.
Second, instead of dividing x and y tilts by 10 for a speed range of -100 to 100, you can divide them by 20 for half the speed range. This will also help, especially when you are getting used to controlling the cyber:bot with the Gripper.
In the radio_tilt_grip_controller script:
if abs(y) > 80: # Change from 80 to 200
In radio the _tilt_grip_controlled_cyberbot script:
fb = y / 10 # change divide by 20 instead of 10 lr = x / 10 # change divide by 20 instead of 10 … if abs(y) > 80: # change to if abs(y) > 200:
A dictionary with key-value pairs is a powerful way to organize data before exchanging it with another device. That’s the main reason why it took so few extra Python statements to add Gripper functionality to the existing Radio Tilt Controlled cyber:bot App.
Here are a few ideas for putting your Tilt Controlled Gripper cyber:bot to use and having some fun!
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/gripper-cyberbot-robot/test-and-tune-gripper
[2] https://learn.parallax.com/tutorials/robot/cyberbot/gripper-cyberbot-robot/go-and-get-object
[3] https://learn.parallax.com/sites/default/files/content/cyberbot/Gripper/Hex/radio_tilt_grip_controlled_cyberbot.hex
[4] https://python.microbit.org/v/2
[5] https://learn.parallax.com/tutorials/robot/cyberbot/add-modules-your-microbit
[6] https://learn.parallax.com/tutorials/robot/cyberbot/how-save-and-reopen-script-python-editor-v2
[7] https://learn.parallax.com/sites/default/files/content/cyberbot/Gripper/Hex/radio_tilt_grip_controller.hex
[8] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control
[9] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/radio-tilt-controlled-cyberbot-app
[10] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/radio-tilt-controlled-cyberbot-app/radio-0
[11] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/radio-tilt-controlled-cyberbot-app/radio