Académique Documents
Professionnel Documents
Culture Documents
Code
Piencoder node
#!/usr/bin/env python
import math
import rospy
import tf
GPIO.setmode(GPIO.BCM)
renc=2
lenc=3
global sl
global sr
sl='b'
sr='f'
GPIO.setup(lenc, GPIO.IN)
GPIO.setup(renc, GPIO.IN)
global lcnt
lcnt=0
global rcnt
rcnt=0
def odl(lenc):
global lcnt
global sl
if sl=='f':
lcnt=lcnt+1
elif sl=='b' :
lcnt=lcnt-1
def rdl(renc):
global rcnt
global sr
if sr=='f':
rcnt=rcnt+1
elif sr=='b' :
rcnt=rcnt-1
GPIO.add_event_detect(lenc,GPIO.RISING)
GPIO.add_event_callback(lenc,odl)
GPIO.add_event_detect(renc,GPIO.RISING)
GPIO.add_event_callback(renc,rdl)
def callback(info):
global lcnt
global lcnt
global sl
global sr
if info.data==0:
sl='f'
sr='f'
elif info.data==1:
sl='b'
sr='b'
elif info.data==2:
sl='b'
sr='f'
elif info.data==3:
sl='f'
sr='b'
def listener():
global lcnt
global rcnt
rospy.init_node('Encoder', anonymous=True)
r = rospy.Rate(1.0)
encdata=Vector3()
encdata.x=lcnt
encdata.y=rcnt
encoder_pub.publish(encdata)
r.sleep()
if __name__ == '__main__':
listener()
Base controller
#!/usr/bin/env python
import rospy
GPIO.setmode(GPIO.BCM)
M1A=14
M1B=4
M2A=15
M2B=18
GPIO.setup(M1A, GPIO.OUT)
GPIO.setup(M1B, GPIO.OUT)
GPIO.setup(M2A, GPIO.OUT)
GPIO.setup(M2B, GPIO.OUT)
def callback(info):
rospy.loginfo("F")
forward()
elif info.data==1:
rospy.loginfo("B")
backward()
elif info.data==2:
rospy.loginfo("L")
turnccw()
elif info.data==3:
rospy.loginfo("R")
turncw()
elif info.data==5:
rospy.loginfo("S")
stop()
else:
rospy.loginfo("S")
stop()
def listener():
# In ROS, nodes are uniquely named. If two nodes witho the same
# run simultaneously.
rospy.init_node('basecontroller', anonymous=True)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
def stop():
GPIO.output(M1A, GPIO.HIGH)
GPIO.output(M1B, GPIO.HIGH)
GPIO.output(M2A, GPIO.HIGH)
GPIO.output(M2B, GPIO.HIGH)
def forward():
GPIO.output(M1A, GPIO.HIGH)
GPIO.output(M1B, GPIO.LOW)
GPIO.output(M2A, GPIO.HIGH)
GPIO.output(M2B, GPIO.LOW)
def backward():
GPIO.output(M1A, GPIO.LOW)
GPIO.output(M1B, GPIO.HIGH)
GPIO.output(M2A, GPIO.LOW)
GPIO.output(M2B, GPIO.HIGH)
def turncw():
GPIO.output(M1A, GPIO.LOW)
GPIO.output(M1B, GPIO.HIGH)
GPIO.output(M2A, GPIO.HIGH)
GPIO.output(M2B, GPIO.LOW)
def turnccw():
GPIO.output(M1A, GPIO.HIGH)
GPIO.output(M1B, GPIO.LOW)
GPIO.output(M2A, GPIO.LOW)
GPIO.output(M2B, GPIO.HIGH)
if __name__ == '__main__':
listener()
Odometry
#!/usr/bin/env python
import math
import rospy
import tf
global s
global sr
global sl
global th
global th0
global x
global y
global x0
global yo
global pl,pr
pl=0
pr=0
def callback(info):
global s
global sr
global sl
global th
global th0
global x
global y
global x0
global yo
global pl,pr
#wt=info.x-pl
#print wt
#print ((info.x-pl)*(3.14159265*0.051)/60)
sl=((info.x-pl)*(3.14159265*0.051)/60)
sr=((info.y-pr)*(3.14159265*0.051)/60)
pl=info.x
pr=info.y
def listener():
global sr
sr=0
global sl
sl=0
global s
s=(sl+sr)/2.0
global th
th=(sr-sl)/15.0
global th0
th0=0
global x
x=0.0
global y
y=0.0
global x0
x0=0.0
global yo
y0=0.0
rospy.init_node('odompublisher', anonymous=True)
rospy.Subscriber("ticks",Vector3, callback)
odom_broadcaster = tf.TransformBroadcaster()
current_time = rospy.Time.now()
last_time = rospy.Time.now()
r = rospy.Rate(1.0)
current_time = rospy.Time.now()
# compute odometry in a typical way given the velocities of the robot
s=(sr+sl)/2.0
print s
th=(sr-sl)/0.15+th0
x=s*cos(th)+x0
y=s*sin(th)+y0
x0=x
y0=y
th0=th
rospy.loginfo("X coordinate%f",x)
rospy.loginfo("Y coordinate%f",y)
rospy.loginfo("Theta coordinate%f",th)
# since all odometry is 6DOF we'll need a quaternion created from yaw
odom_broadcaster.sendTransform(
(x, y, 0.),
odom_quat,
current_time,
"base_link",
"odom"
#odom = Odometry()
#odom.header.stamp = current_time
#odom.header.frame_id = "odom"
#odom.child_frame_id = "base_link"
#odom.twist.twist = Twist(Vector3(vx, vy, 0), Vector3(0, 0, vth))
#odom_pub.publish(odom)
last_time = current_time
#rospy.spin()
r.sleep()
if __name__ == '__main__':
listener()
piremote
#!/usr/bin/env python
import rospy
def talker():
rospy.init_node('piremote', anonymous=True)
msg=getch()
print msg
if msg=='w':
msg=0
elif msg=='q':
break
elif msg=='s':
msg=1
elif msg=='a':
msg=2
elif msg=='d':
msg=3
else:
msg=5
rospy.loginfo(msg)
pub.publish(msg)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
Tf broadcast
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
ros::NodeHandle n;
ros::Rate r(10);
tf::TransformBroadcaster broadcaster;
while(n.ok()){
broadcaster.sendTransform(
tf::StampedTransform(
ros::Time::now(),"base_link", "laser"));
r.sleep();