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/v/2 editor.  The cyberbot.py module was also added to the Project Files as shown in Add modules to your micro:bit.  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.

 

  • Connect the micro:bit with battery pack to the USB cable
  • Click Connect, then click Flash, then click Disconnect.
  • 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.

 

Project Script: radio_tilt_grip_controller

# 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)