一:前期准备工作: 硬件部分: (1)材料: 树莓派3b+,超声波测距模块,手机(提供热点),towerpro sg 5100电机三个,towerpro sg 90电机一个,杜邦线若干,面包板,电阻,四个五号电池连起来的电池盒两个,五号电池八节,铝板,亚克力板,螺丝螺帽若干, (2)图纸: 机器人本体部分: 
各条腿: 

制备安装好之后是这个样子的: 
软件部分: (1)先配置树莓派,开启远程连接,由于步骤过于简单,在此省略,请参照2制作日志或登陆树莓派实验室之初级教程。 详见树莓派实验室初级教程, 1定义:伺服马达受不同长度的脉冲控制。基本上可以这样理解,伺服电机接收到1个脉冲,就会旋转1个脉冲对应的角度,从而实现位移。详细定义请自行百度, 2树莓派如何操作: 在这个网址下,有关于树莓派上操作gpio端口的全部详细解释,https://www.cnblogs.com/dongxiaodong/p/9877734.html 简单概括为:树莓派的管脚有两种命名方式,分别为wpi和bcm码,需要在代码中说明, 以下是一个实例,示范调用代码的格式 1、首先对 RPi.GPIO 进行设置 | import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) #物理引脚编码 GPIO.setup(12, GPIO.OUT) |
2、设置某个输出针脚状态为高电平: | GPIO.output(12, GPIO.HIGH) # 或者 GPIO.output(12, 1) # 或者 GPIO.output(12, True) |
3、设置某个输出针脚状态为低电平: | GPIO.output(12, GPIO.LOW) # 或者 GPIO.output(12, 0) # 或者 GPIO.output(12, False) |
4、程序结束后进行清理 二:开发程序: 了解了对于gpio接口的控制之后我们开始写程序, 源程序在附于最后,写完后使用树莓派连接,进入目标文件夹 即: cd Desktop Cd pythfiles 执行命令:python hexapod1.py m f 即可使机器人前进,连线图如下图所示 实际的布线图如下图所示 
最终成品视频附于附件一 源代码: - <font style="font-size: 15pt">import RPi.GPIO as GPIO
- import pigpio
- import time
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- tilt = 4
- br = 21
- bl = 6
- trig = 23
- echo = 24
- GPIO.setup(trig, GPIO.OUT)
- GPIO.setup(echo, GPIO.IN)
- pi = pigpio.pi()
- def backward():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def forward():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def left():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def right():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
-
- def stop():
- pi.set_servo_pulsewidth(tilt, 0)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 0)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 0)
- time.sleep(0.15)
-
- return
- def obstacleDetected():
- backward()
- backward()
- backward()
- backward()
- backward()
- right()
- right()
- right()
-
- return
- def turnHead():
- pi.set_servo_pulsewidth(head, 700)
- time.sleep(0.5)
- pi.set_servo_pulsewidth(head, 2100)
- time.sleep(0.5)
- pi.set_servo_pulsewidth(head, 1500)
- time.sleep(0.5)
- return
- def autoMode():
- print ("Running in auto mode!")
- turnHead()
-
- time.sleep(0.5)
- GPIO.output(trig, 0)
- time.sleep(0.5)
-
- GPIO.output(trig,1)
- time.sleep(0.00001)
- GPIO.output(trig,0)
-
- while GPIO.input(echo) == 0:
- pulse_start = time.time()
-
- while GPIO.input(echo) == 1:
- pulse_end = time.time()
- pulse_duration = pulse_end - pulse_start
-
- distance = pulse_duration * 17150
-
- distance = round(distance, 2)
-
- if distance > 1 and distance < 35:
- obstacleDetected()
- else:
- forward()
- forward()
- forward()
-
- pi.set_servo_pulsewidth(head, 2100)
- time.sleep(0.5)
- return
-
- def manualMode():
-
- move = str(sys.argv[2])
- if move == "F" or move == "f":
- print("Moving forward!")
- forward()
- elif move == "B" or move == "b":
- print("Moving backward!")
- backward()
- elif move == "L" or move == "l":
- print("Moving left!")
- left()
- elif move == "R" or move == "r":
- print("Moving right!")
- right()
- else:
- print("Invalid argument!")
-
- return
-
- def main():
- opt = str(sys.argv[1])
-
- if opt == "A" or opt == "a":
- autoMode()
- elif opt == "M" or opt == "m":
- manualMode()
-
- return
-
- while True:
- main()
- GPIO.cleanup()
- pi.stop()
- </font>
复制代码
以上的Word格式文档51黑下载地址:
3六足机器人制作文档.docx
(1.8 MB, 下载次数: 29)
|