Script for Tilt & Grip Controller

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.

  • Right-click the radio_tilt_grip_controller.hex link below, choose Save As, and then save the file to your computer.

radio_tilt_grip_controller

The script was created by entering the text below into the python.microbit.org editor.  The Script Name was set to cyber_bot_gripper_forward_object before saving it as a .hex file as shown in Save & Edit Scripts.

Project Script: radio_tilt_grip_controller

  • Connect the micro:bit with battery pack to the USB cable.  (You can also use a second cyber:bot for this.)
  • If you changed tilt controlled cyber:bot script's channel=7 statement, make sure to change this one to match.
  • Click Send to micro:bit.
  • Disconnect the USB from the micro:bit module.
  • Make sure the battery holder is connected to the micro:bit, and that it has some relatively new alkaline AA batteries.  Or, if you are using a cyber:bot as a tilt controller, make sure its batteries are connected and that the cyber:bot board's PWR switch is set to 1.
# 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)