我去年从www.thingiverse.com上下载了MrCrankyface设计的V4 truck车架,自己设计了平台,用3D打印完毕后完成组装并安装了下列设备:
1 Fubata S3003 舵机
2 JGB37-520直流减速电机(为了保证不伤到小孩,特意买了低转速大扭矩的电机)
3顽皮龙模型车的电调ESC(10A)
4 PCA9685 PWM舵机驱动板(同时也能驱动ESC)
5 18650电池一对和电池盒一个
6 DC降压电源模块(负责树莓派供电)
7 LM2596S直流可调降压模块(负责PCA9685供电)
8 CSI 接口摄像头(支持树莓派)
9 树莓派3B+
树莓派上的程序如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
import Adafruit_PCA9685
import time
from std_msgs.msg import String
class car():
angel_min=200
angel_max=600
angel_middle=340
forword_max=230#前进的最大速度,但是最小的pwm值
back_max=560#后退的最大速度
stop=380#停止的pwm值
angel_port=0#舵机在一号口
throtle_port=1#esc在二号口
pwm = Adafruit_PCA9685.PCA9685()
speed=stop
angel=angel_middle
fre_step=20#操作幅度
def __init__(self):
#设置初始值
self.pwm.set_pwm_freq(60)
self.pwm.set_pwm(self.angel_port,0,self.angel)
self.pwm.set_pwm(self.throtle_port,0,self.speed)
print('初始化完成!')
def receive(self,data):
#w加速 s减速 a左转 d右转 q 刹车
if data=='w':
self.speed=self.speed-self.fre_step
if self.speed<=self.forword_max:
self.speed=self.forword_max
#减少pwm值 增加前进速度值直至从后退变前进,同时限制前进最大速度
elif data=='s':
self.speed=self.speed+self.fre_step
if self.speed>=self.back_max:
self.speed=self.back_max
#增大pwm值 减少前进速度值直至从前进变后退,同时限制后退最大速度
elif data=='q':
self.speed=self.stop
elif data=='a':
self.angel=self.angel+self.fre_step
if self.angel>=self.angel_max:
self.angel=self.angel_max
elif data=='d':
self.angel=self.angel-self.fre_step
if self.angel<=self.angel_min:
self.angel=self.angel_min
self.pwm.set_pwm(self.throtle_port,0,self.speed)
self.pwm.set_pwm(self.angel_port,0,self.angel)
donkeycar=car()
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
donkeycar.receive(data.data)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# name are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()
保存后,记得修改为可执行文件,用rosrun运行即可。
笔记本上的程序如下:
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String
import sys
from pynput import keyboard
hello_str=''
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
def on_press(key):
try:
hello_str=format(key.char)
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
except AttributeError:
print('special key {0} pressed'.format(key))
def on_release(key):
if key == keyboard.Key.esc:
return False
def on_release(key):
if key == keyboard.Key.esc:
return False
def talker():
while not rospy.is_shutdown():
with keyboard.Listener(on_press = on_press,on_realse=on_release) as listener:
listener.join()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
exit()
保存后,修改为可执行文件,用rosrun运行即可。我的小车可以跑起来了!这是人类的一小步,是我的一大步。