图1-1,你的手动小车
代码是
#机器人驱动主程序
#请在main中编写您自己的机器人驱动代码
import tkinter as tk
import turtle
v=0 # 速度
acc=False;slow=False;left=False;right=False # 按键状态
step=0.5 # 一次速度变化量
def keyup_press(event):global acc;acc=True
def keyup_release(event):global acc;acc=False
def keydown_press(event):global slow;slow=True
def keydown_release(event):global slow;slow=False
def keyleft_press(event):global left;left=True
def keyleft_release(event):global left;left=False
def keyright_press(event):global right;right=True
def keyright_release(event):global right;right=False
def stop_all(event):global v,acc,slow,left,rightv=0;acc=slow=left=right=False
def main():print("你知道吗,这个的作者其实是溥哥")turtle.pensize(5)turtle.color('green')turtle.setup(width=0.2,height=0.3,startx=100,starty=100)turtle.penup()global vroot=tk.Tk()root.bind_all("<KeyPress-Up>",keyup_press)root.bind_all("<KeyPress-Down>",keydown_press)root.bind_all("<KeyPress-Left>",keyleft_press)root.bind_all("<KeyPress-Right>",keyright_press)root.bind_all("<KeyRelease-Up>",keyup_release)root.bind_all("<KeyRelease-Down>",keydown_release)root.bind_all("<KeyRelease-Left>",keyleft_release)root.bind_all("<KeyRelease-Right>",keyright_release)root.bind_all("<KeyPress-Q>",stop_all)root.bind_all("<KeyPress-q>",stop_all)_v=tk.StringVar();_operations=tk.StringVar()tk.Label(root,text="机器人控制面板",font=(None,0,"bold")).pack()tk.Label(root,textvariable=_v).pack()tk.Label(root,textvariable=_operations).pack()while True:x=300#比例尺,大小详,小大略if acc:v+=stepif slow:v-=stepif left: # 左转robot_drv.set_motors(1,-3,2,int(v),3,-3,4,int(v))elif right:robot_drv.set_motors(1,int(v),2,-3,3,int(v),4,-3)else:robot_drv.set_motors(1,int(v),2,int(v),3,int(v),4,int(v))root.update()turtle.goto(int(robot_drv.get_gps(32)[0]/x),int(robot_drv.get_gps(32)[1]/x))turtle.pendown()_v.set("速度: %d" % v)_op="当前操作: "if acc:_op+="加速 "if slow:_op+="减速 "if left:_op+="左转 "if right:_op+="右转 "_operations.set(_op)# *********************************************
# 以下为初始化代码,请不要修改或者删除
# *********************************************
import sys
import irobotq_robotdriver as robot_drvif __name__ == '__main__':try:ret=robot_drv.init(sys.argv[1],sys.argv[2],int(sys.argv[3]))if(ret == 0):main()robot_drv.over()print("机器人程序运行结束")else:print('初始化错误,错误码:%d' % ret)print('按任意键退出')input()except Exception as e:import traceback;traceback.print_exc()print('按任意键退出')input()
原理解释:手动控制之前发过
重点是海龟库,应用了海龟库的坐标绘画和画笔粗细及颜色调节还有窗口大小
tips:python模式仅限于1.6.1.8以上(不包括1.6.2.2和1.6.2.4版本,此bug还未修复)
更改比例尺大小,根据不同地图调节