小龟机器人Python教学案例
本帖最后由 捣腾一个机器人 于 2023-6-7 17:24 编辑前言:
小龟机器人可以轻松学习机器人编程,不管是图形化编程,还是Python编程、Web编程,它都行。小龟同时开发了一体化编程平台,通过浏览器就可以直接进行编程,省去了下载安装的烦恼,让广大师生可以更轻松更愉快的学习机器人。
本文介绍小龟的Python机器人编程案例,辅以图形化编程帮助参考理解,主要由两部分组成:
1、Python教学案例介绍在教学上的实际应用;
2、Python编辑器介绍在线和离线双模式下的使用方法。
小龟机器人Python教学案例
案例1 使用超声波模块实现近距离告警提示
知识点:
变量、循环、逻辑、音频
Python代码:
d = None
while True:
sleep(0.001)
d = car.hcsr04(A1, A2)
if d <= 10:
car.buzzer.fire()
else:
car.buzzer.music('''1=''')
sleep(1)
图形化编程:
案例2 跑马灯
知识点:
列表、色彩
Python代码:
i = None
for i in , 16),
int('0x' + '#33ff33', 16),
int('0x' + '#000099', 16),
int('0x' + '#000000', 16)]:
sleep(0.001)
car.led.on(i)
sleep(0.75)
图形化编程:
案例3 小车精准右转90度
知识点:
角度积分、绝对值、逻辑、电机运动
Python代码:
import math
car.motion.start_offset()
car.motion.start_rpy()
car.right(30)
while True:
sleep(0.001)
if math.fabs(car.motion.rpy()) >= 90:
car.stop()
break
图形化编程:
案例4 按键控制LED灯
知识点:
循环、逻辑、色彩、人机互动
Python代码:
while True:
sleep(0.001)
sleep(0.01)
if (car.touch.is_pressed(A)) and (car.touch.is_pressed(B)):
car.led.on(int('0x' + '#0000ff', 16))
elif car.touch.is_pressed(B):
car.led.on(int('0x' + '#ff0000', 16))
elif car.touch.is_pressed(A):
car.led.on(int('0x' + '#33ff33', 16))
else:
car.led.off()
图形化编程:
案例5 WS2812灯组逐个亮起
知识点:
变量递增、WS2812
Python代码:
E7_81_AF_E7_BB_841 = None
i = None
_E7_81_AF_E7_BB_841 = * 64
def hex2rgb(color):
r = color >> 16
g = color >> 8 & 0x00FF
b = color & 0x0000FF
return (r,g,b)
def set_brightness(brightness,r,g,b):
r = r*brightness//100
g = g*brightness//100
b = b*brightness//100
return (r,g,b)
def rgb2hex(r,g,b):
return r << 16 | g << 8 | b
def breathe(l, color, start_led=0, end_led=64, speed = 0.02, breathe_times=1, min_rightness=0, max_rightness=64, pin="A1"):
rgb_color = hex2rgb(color)
for k in range(breathe_times):
for i in range(min_rightness, max_rightness):
rgb_color_brightness = set_brightness(i, *rgb_color)
hex_color = rgb2hex(*rgb_color_brightness)
for j in range(start_led, end_led):
l = hex_color
car.ws2812.write(pin, l)
sleep(speed)
for i in range(max_rightness, min_rightness-1, -1):
rgb_color_brightness = set_brightness(i, *rgb_color)
hex_color = rgb2hex(*rgb_color_brightness)
for j in range(start_led, end_led):
l = hex_color
car.ws2812.write(A1, l)
sleep(speed)
for i in range(1, 65):
sleep(0.001)
_E7_81_AF_E7_BB_841 = int('0x' + '#3366ff', 16)
car.ws2812.write(A1, _E7_81_AF_E7_BB_841)
sleep(0.02)
图形化编程:
案例6 舵机组并行运动
知识点:
舵机运动、舵机组运动、串行控制、并行控制
Python代码:
car.servo(,[
,
,
,])
图形化编程:
案例7 在屏幕指定坐标位置显示文字
知识点:
坐标、屏幕显示
Python代码:
car.screen.print(str('ABC'), 0, 0)
图形化编程:
案例8 四足前进运动
知识点:
舵机组运动、四足步态
Python代码:
import math
class Dog:
def __init__(self, pins, values, directions, leg_length, leg_space, leg_space_offside):
self.pins = pins
self.values = values
self.directions = directions
self.leg_length = leg_length
self.leg_space = leg_space
self.leg_space_offside = leg_space_offside
def xpos(self, x1, x2, x3, x4, t1=0.5, t2=0):
v1 = self.values
v2 = self.values
v3 = self.values
v4 = self.values
o1 = self.x2offset(x1)
o2 = self.x2offset(x2)
o3 = self.x2offset(x3)
o4 = self.x2offset(x4)
d1 = self.directions
d2 = self.directions
d3 = self.directions
d4 = self.directions
return
def zpos(self, z1, z2, z3, z4, t1=0.5, t2=0):
v1 = self.values
v2 = self.values
v3 = self.values
v4 = self.values
o1 = self.z2offset(z1)
o2 = self.z2offset(z2)
o3 = -self.z2offset(z3)
o4 = -self.z2offset(z4)
d1 = self.directions
d2 = self.directions
d3 = self.directions
d4 = self.directions
return
def set_x(self, dx, t=0.5):
car.servo(self.pins, [
self.xpos(-dx, -dx, -dx, -dx, t)
])
sleep(t)
def set_z(self, height, t=0.5):
_h = self.leg_length + height
car.servo(self.pins, [
self.zpos(_h, _h, _h, _h, t)
])
sleep(t)
def x2offset(self, dx):
offsetRad = math.asin(dx/self.leg_length)
offsetDeg = offsetRad * (180 / math.pi)
return offsetDeg
def z2offset(self, dz):
offsetRad = math.acos(dz/self.leg_length)
offsetDeg = offsetRad * (180 / math.pi)
return offsetDeg
def rotate_x(self, deg, t=0.5):
rad = deg * (math.pi / 180)
h = self.leg_space_offside / 2 * math.sin(rad)
_l = self.leg_length / 1.414
car.servo(self.pins, [
self.zpos(_l + h, _l - h, _l + h, _l - h, t)
])
sleep(t)
def rotate_y(self, deg, t=0.5):
rad = deg * (math.pi / 180)
h = self.leg_space / 2 * math.sin(rad)
_l = self.leg_length / 1.414
car.servo(self.pins, [
self.zpos(_l + h, _l + h, _l - h, _l - h, t)
])
sleep(t)
def go(self, dx, interval=0.5, keep=1):
times = keep / (interval*4)
_times = math.floor(times)
for i in range(times):
car.servo(self.pins, [
self.xpos(0,dx,0,-dx, interval),
self.xpos(dx,dx,0,-dx, interval),
self.xpos(dx,0,-dx,0, interval),
self.xpos(dx,dx,-dx,0, interval),
], 0)
sleep(interval*4)
car.servo(self.pins, )
def back(self, dx, interval=0.5, keep=1):
times = keep / (interval*4)
_times = math.floor(times)
for i in range(times):
car.servo(self.pins, [
self.xpos(0,dx,0,-dx, interval),
self.xpos(0,dx,0-dx,-dx, interval),
self.xpos(dx,0,-dx,0, interval),
self.xpos(dx,0,-dx,-dx, interval),
], 0)
sleep(interval*4)
car.servo(self.pins, )
def left(self, dx, interval=0.5, keep=1):
times = keep / (interval*4)
_times = math.floor(times)
for i in range(times):
car.servo(self.pins, [
self.xpos(0,dx,0,-dx, interval),
self.xpos(-dx,dx,0,-dx, interval),
self.xpos(-dx,0,dx,0, interval),
self.xpos(-dx,dx,dx,0, interval),
], 0)
sleep(interval*4)
car.servo(self.pins, )
def right(self,dx, interval=0.5, keep=1):
times = keep / (interval*4)
_times = math.floor(times)
for i in range(times):
car.servo(self.pins, [
self.xpos(dx,0,-dx,0, interval),
self.xpos(dx,-dx,-dx,0, interval),
self.xpos(0,-dx,0,dx, interval),
self.xpos(dx,-dx,0,dx, interval),
], 0)
sleep(interval*4)
car.servo(self.pins, )
pins =
values =
dirs = [-1, +1, -1, +1]
leg_len = 34
leg_space=45
leg_space_offside=95
dog = Dog(pins, values, dirs, leg_len, leg_space, leg_space_offside)
dog.go(15,0.5,3)
图形化编程:
案例9 小车通过识别二维码运动
知识点:
机器视觉、图像处理、变量、逻辑、循环、文本
Python代码:
code = None
car.vision.on('Y')
while True:
sleep(0.001)
car.vision.snap()
code = car.vision.qrcode()
if code == 'GO':
car.go(30, 2)
sleep(2)
if code == 'LEFT':
car.left(30, 2)
sleep(2)
sleep(1)
图形化编程:
小龟机器人Python编辑器
在线模式:
登录网址:http://guidan.com/ide2/
离线模式:
1、连接小龟机器人自身热点:xiaogui-xxxx或小车-xxxx(固件版本不同显示差异,若提示该网络不能上网请选择继续使用该网络)
2、登录:192.168.4.1,页面显示如下
3、点击Python即可进行Python教学和练习
厉害厉害 赞赞赞赞赞! 厉害厉害 不错,教程很详细! 谢谢分享谢谢分享 教程太详细了
页:
[1]