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 and lift and lower and open.
For those who have a class set of cyber:bot robots, a second cyber:bot with batteries 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 Feedback 360 drive servos and Gripper servo.
radio_tilt_grip_controlled_cyberbot_with_fb360.hex [3]
The .hex file download was created by entering the Python script below into the python.microbit.org/v/2 editor, and also adding the cyberbot.py and Feedback360.py modules to the Project File as shown in Add Modules to your micro:bit. [5] The Script Name was set to radio_tilt_grip_controller 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_with_fb360 from cyberbot import * from feedback360 import * import radio drive.connect() 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 = 0 vR = 0 if button == 'B': # added bot(15).servo_angle(30) # added Gripper close & lift elif button == 'A': # added bot(15).servo_angle(150) # added Gripper lower & open finally: drive.speed(vL/2, -vR/2)
This is the script to run on the micro:bit module serving as the tilt controller for the Gripper-equipped cyber:bot robot with Feedback 360° servos. It takes measurements from its onboard tilt sensor and sends radio data values to the cyber:bot for its Feedback 360° drive servos and Gripper servo.
radio_tilt_grip_controller.hex [7]
The .hex file download was created by entering the Python script below into the python.microbit.org/v/2 editor, and also adding the radio and math modules to the Project File as shown in Add Modules to your micro:bit. [5] The Script Name was set to radio_tilt_grip_controller 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 Gripper control features of the remote controlled cyber:bot with Gripper and Feedback 360° servos.
The scripts in this activity are just slightly expanded versions of the ones from the Cybersecurity: Radio Tilt Control [8] tutorial’s Radio Tilt Controlled cyber:bot App [9] activity.
First, the script had to be ported from continuous rotation servo control to Feedback 360° servo control. The changes to the script that have to be made to work with the new servos are explained in the How to Update Scripts for Feedback 360° Servos [10] activity.
To add button control to the Radio Tilt Controller Code [11], the routine below was added to the transmitting tilt controller 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.
button = dictionary['button'] # added
These lines were added to respond to the button values by opening or closing the Gripper.
if button == 'B': # added bot(15).servo_angle(30) # added Gripper close & lift elif button == 'A': # added bot(15).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 "stay still" range where there’s no 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 on the micro:bit tilt controller.
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 is helpful especially when you are getting used to controlling the cyber:bot with the Gripper.
In radio_tilt_grip_controller:
if abs(y) > 80: # Change from 80 to 200
In radio_tilt_grip_controlled_cyberbot:
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 project.
Here are a few ideas for putting your Tilt Controlled Gripper cyber:bot with Feedback 360° servos to use and having some fun!
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/tilt-control-gripper-cyberbot-feedback-360%C2%B0-servos/test-and-tune-gripper
[2] https://learn.parallax.com/tutorials/robot/cyberbot/tilt-control-gripper-cyberbot-feedback-360%C2%B0-servos/go-and-get-object-speed
[3] https://learn.parallax.com/sites/default/files/content/cyberbot/GripperF360/Hex/radio_tilt_grip_controlled_cyberbot_with_fb360.hex
[4] https://python.micrbit.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/GripperF360/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/upgrade-your-cyberbot-feedback-360-servos/how-update-scripts-feedback-360-1
[11] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/radio-tilt-controlled-cyberbot-app/radio-0