Script for Tilt & Grip cyber:bot

Script for Tilt & Grip Controlled cyber:bot

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.

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

radio_tilt_grip_controlled_cyberbot.hex


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.

 

  • Plug the battery pack’s barrel plug into the cyber:bot board’s barrel jack.
  • Set the cyber:bot board’s PWR switch to 1.
  • Click Connect, then Flash.
  • Disconnect the USB from the cyber:bot robot’s micro:bit module.

 

Project Script: radio_tilt_grip_controlled_cyberbot

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