Bot:bit

来自Labplus盛思维基百科
Senadmin讨论 | 贡献2018年1月12日 (五) 10:54的版本 Motion Bot python编程
跳转至: 导航搜索
Cc bot2.jpg

概述

CC-Bot人形机器人是一款可以编程控制的人形机器人,支持Mpython图形化编程和Python代码编程,简单易学,通过编程可拓展人的空间思维空间能力。机器人具有多种工作形态,一是装4自由度舵机作为手臂,加上2路电机作为轮子,自由移动的同时双手可灵活操作;二是装有4自由度舵机作为双腿、实现自由行走、避障;三是同时组转双手和双腿,8自由度舵机实现各种舞蹈动作,使用方法灵活多变,富有创意。

产品特点

  • 动作灵活:全身拥有8个动作关节,拟人造型,实现动作舞蹈,身手灵活。
  • 无线遥控:2.4GHz射频传输模块,可实现遥控格斗运动,欢乐无穷。
  • 内置加速度计
  • 测距避障:内置的高精度超声波模块,能快速返回测量信息、走迷宫、越野的多种任务挑战。
  • 高续航:双节14500锂电池,可循环充电,支持课堂教学应用。


百变创意玩法

扎气球
写字画画
广场舞
运动对抗

程序下载教程

Step1.CC-Bot USB接口连接至电脑,打开电源开关。电脑应能识别mbed端口设备,如提示"未能安装成功驱动",须下载安装mbedSerial

Mbed串口.png

Step2.使用m:python编译器编写程序,编辑完成后,点击「下载Hex」。

下载Hex.png

Step3.Hex文件保存至MICROBIT 可移动存储设备中即下载固件成功。

保存hex.png

CC-Bot 拼装形态

Walk Bot

  • 4自由度舵机作为双腿、实现自由行走
Walk Bot

Walk Bot python编程

Cc_bot_dancing程序下载

# -*- coding: utf-8 -*-
from microbit import *
import music
import math
servo_pos = bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])
def setServo(servo, angle):
    "set the servo angel"
    a = (1.5 + angle/90) * 1000
    servo_pos[servo*2 + 1] = int(a / 256)
    servo_pos[servo*2 + 2] = int(a % 256)

def updatePosition():
    servo_pos[0] = 0
    i2c.write(0x2A, servo_pos)    
    
def getDistance():
    i2c.write(0x0b, bytearray([1]))
    temp=i2c.read(0x0B,2)
    dis =(temp[0]+temp[1]*256)/10
    return dis
inc = 0
phase_start=[0, 0, 0, 0]
phase=[0, 0, 0, 0]
offset=[0, 0, 0, 0]
amplitude=[0, 0, 0, 0]
t = 0
def refresh():
    global t, phase, inc, amplitude, phase_start
    if (running_time() - t) > 50:
        t = running_time()
        for i in range(0, 4):
            pos = round(amplitude[i]*math.sin(phase[i] + phase_start[i]) + offset[i])
            setServo(i, pos)
            phase[i] = phase[i] + inc
        updatePosition()

def action(A, O, DIFF, T, steps):
    global inc, amplitude, phase_start, offset
    t2 = 0
    inc = 2*math.pi/(T/50)
    for i in range(0, 4):
        amplitude[i] = A[i]
        phase_start[i] = DIFF[i]
        offset[i] = O[i]
    cycle = int(steps)
    t2 = running_time() + T*cycle
    while (running_time() < t2):
        refresh()
    for i in range(0, 4):
        amplitude[i] = A[i]
        phase_start[i] = DIFF[i]
        offset[i] = O[i]
    # move the servo
    t2 = running_time() + T*(steps - cycle)
    while (running_time() < t2):
        refresh()
def walking(steps, T=1000, dir=1):
    AMP = (30, 30, 20, 20)
    OFFSET = (0, 0, 4, -4)   
    DIFF = (0, 0, -math.pi/2 * dir, -math.pi/2 * dir)
    action(AMP, OFFSET, DIFF, T, steps)
    return 
def turn(steps, T=2000, dir=1):
    OFFSET = [0, 0, 4, -4]   
    DIFF = (0, 0, -math.pi/2 * dir, -math.pi/2 * dir)
    if dir == 1:
        AMP = (30, 10, 20, 20)
    else:
        AMP = (10, 30, 20, 20)
    action(AMP, OFFSET, DIFF, T, steps)
    return 
def moonwalker(steps, T=900, h=20, dir=1):
    'Moonwalker. Otto moves like Michael Jackson'
    AMP = [0, 0, h, h]
    OFFSET = [0, 0, h/2 + 2, -h/2 -2]   
    DIFF = [0, 0, math.pi/180*dir*-90, math.pi/180*dir*-150]
    action(AMP, OFFSET, DIFF, T, steps)
    return 
def crusaito(steps, T, h, dir):
    AMP = [25, 25, h, h]
    OFFSET = [0, 0, h/2+ 4, -h/2 - 4]   
    DIFF = [90, 90, 0, math.pi/180*dir*-60]
    action(AMP, OFFSET, DIFF, T, steps)
    
def flapping(steps, T, h, dir):
    AMP = [12, 12, h, h]
    OFFSET = [0, 0, h-10, -h+10]   
    DIFF = [0, math.pi/180*180, math.pi/180*dir*-90, math.pi/180*dir*90]
    action(AMP, OFFSET, DIFF, T, steps)
    return    
servo_position = [0, 0, 0, 0]
servo_increment = [0, 0, 0, 0]
def moveServos(time, servo_target):
    if time > 20:
        for i in range(0, 4):
            servo_increment[i] = (servo_target[i] - servo_position[i])/(time/20)       
        final_time = running_time() + time;
        iteration = 1
        while running_time() < final_time:
            partial_time = running_time()+20
            for i in range(0, 4):
                setServo(i, servo_position[i]+iteration*servo_increment[i])
            updatePosition()
            while running_time() < partial_time:
                pass
            iteration = iteration+1
    else:
        for i in range(0, 4):
            setServo(i, servo_target[i])
        updatePosition()
    for i in range(0, 4):
        servo_position[i] = servo_target[i]
    return 
def jump(steps, T):
    up = [0, 0, 45, -45]
    moveServos(T, up)
    down = [0, 0, 0, 0]
    moveServos(T, down)
    return
def home():
    for i in range(0, 4):
        setServo(i, 0)
        servo_position[i] = 0
    updatePosition()
    
display.off()
home()
while True:
    walking(5, 1500, 1)
    walking(5, 1500, -1)
    music.play(music.BA_DING)
    moonwalker(5, 1000, 25, 1)
    moonwalker(5, 1000, 25, -1)    
    music.play(music.BADDY)
    crusaito(8, 1000, 15, 1)
    crusaito(8, 1000, 15, -1)
    crusaito(4, 2000, 15, 1)
    crusaito(4, 2000, 15, -1)  
    music.play(music.NYAN)
    flapping(5, 1500, 15, 1)
    flapping(5, 1500, 15, -1)
    music.play(music.BIRTHDAY)

Motion Bot

  • 4自由度舵机作为手臂,加上2路电机作为轮子,自由移动的同时双手可灵活操作
motion_bot

Motion Bot python编程

  • 程序说明:需要下载有两个程序,一个是micro:bit作为遥控程序,另一个为cc bot执行动作程序。micro:bit实时发送xyz加速度数据操控cc bot轮子行走,按下「A」按键+xyz加速度操控两臂动作。

motion_bot程序下载

  • micro:bit遥控程序
from microbit import *
import radio
radio.on()
radio.config(length=8, queue=3, channel=79, power=7, 
             address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT)
    
msg = bytearray(8)
x = 0
y = 0
z = 0
a = 0
while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    z = accelerometer.get_z()
    if button_a.is_pressed():
        a = a | 0x01
    else:
        a = a & 0xFE
    if button_b.is_pressed():
        a = a | 0x02
    else:
        a = a & 0xFD
    x = x + 10000;
    msg[0] = int(x / 256)
    msg[1] = x % 256
    y = y + 10000;
    msg[2] = int(y / 256)
    msg[3] = y % 256
    z = z + 10000;
    msg[4] = int(z / 256)
    msg[5] = z % 256
    msg[6] = int(a / 256)
    msg[7] = a % 256
    radio.send_bytes(msg)
    sleep(100)
  • cc bot执行程序
# -*- coding: utf-8 -*-
from microbit import *
import radio
import math

motor_pwm = bytearray([8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
servo_pos = bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])

'''
motor_pwm.ch1 = M1.A
motor_pwm.ch2 = M1.B
motor_pwm.ch3 = M2.A
motor_pwm.ch4 = M2.B
'''

def motion(leftSpeed, rightSpeed):
    if leftSpeed > 2000:
        leftSpeed = 2000
    if leftSpeed < -2000:
        leftSpeed = -2000
    if leftSpeed == 0:
        motor_pwm[1] = 0
        motor_pwm[2] = 0
        motor_pwm[3] = 0
        motor_pwm[4] = 0
    if leftSpeed > 0:
        motor_pwm[1] = int(leftSpeed / 256)
        motor_pwm[2] = int(leftSpeed % 256)
        motor_pwm[3] = 0
        motor_pwm[4] = 0
    if leftSpeed < 0:
        leftSpeed = -leftSpeed
        motor_pwm[1] = 0
        motor_pwm[2] = 0
        motor_pwm[3] = int(leftSpeed / 256)
        motor_pwm[4] = int(leftSpeed % 256)
    if rightSpeed > 2000:
        rightSpeed = 2000
    if rightSpeed < -2000:
        rightSpeed = -2000
    if rightSpeed == 0:
        motor_pwm[5] = 0
        motor_pwm[6] = 0
        motor_pwm[7] = 0
        motor_pwm[8] = 0
    if rightSpeed > 0:
        motor_pwm[5] = 0
        motor_pwm[6] = 0
        motor_pwm[7] = int(rightSpeed / 256)
        motor_pwm[8] = int(rightSpeed % 256)
    if rightSpeed < 0:
        rightSpeed = -rightSpeed
        motor_pwm[5] = int(rightSpeed / 256)
        motor_pwm[6] = int(rightSpeed % 256)
        motor_pwm[7] = 0
        motor_pwm[8] = 0
    i2c.write(0x2A, motor_pwm)


def setServo(servo, angle):
    "set the servo angel"
    a = (1.5 + angle/90) * 1000
    servo_pos[servo*2 + 1] = int(a / 256)
    servo_pos[servo*2 + 2] = int(a % 256)


def updatePosition():
    servo_pos[0] = 0
    i2c.write(0x2A, servo_pos)


def getDistance():
    i2c.write(0x0b, bytearray([1]))
    temp = i2c.read(0x0B, 2)
    dis = (temp[0]+temp[1]*256)/10
    return dis

# application
display.off()
motion(0, 0)

radio.on()
radio.config(length=8, queue=20, channel=79, power=7,
             address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT)
x = 0
y = 0
z = 0
a = 0
left = 0
right = 0
while True:
    # print("running")
    msg = bytes(8)
    msg = radio.receive_bytes()
    if msg is not None:
        x = msg[0]*256 + msg[1]
        x = x - 10000
        y = msg[2]*256 + msg[3]
        y = y - 10000
        z = msg[4]*256 + msg[5]
        z = z - 10000
        a = msg[6]*256 + msg[7]
        if a == 0:
            left = int((y + x)/1000 * 2000)
            right = int((y - x)/1000 * 2000)
            # print('left = ', left)
            # print('right = ', right)
            motion(right, left)
        if (a & 0x03) != 0:
            motion(0, 0)
            y = min(max(-1000, y), 1000)
            x = min(max(-1000, x), 1000)
            sv = math.asin(y/1000)*180/math.pi
            sh = math.asin(x/1000)*180/math.pi
            sv = min(max(-45, sv), 45)
            sh = min(max(-45, sh), 45)
            if (a & 0x01) != 0:
                setServo(0, sv)
                setServo(1, -sh)
            if (a & 0x02) != 0:
                setServo(2, -sv)
                setServo(3, sh)
            updatePosition()