lemueltra

ros_controle

Oct 9th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4.  
  5. from std_msgs.msg import Float64
  6.  
  7. angulo = 0.0;
  8. setpoint = 30.0
  9. erro = 0
  10. termo_pd = 0
  11. termo_pd_escalonado = 0
  12.  
  13. def posi_callback(msg):
  14.     global angulo
  15.     angulo = msg.data
  16.     #rospy.loginfo("Recebi: " % (posicao))
  17.  
  18. def corr_callback(msg):
  19.     global corrente
  20.     mVperAmp = 100;
  21.     ACSoffset = 2500;
  22.    
  23.     corrente = msg.data
  24.     mV = (corrente / 1024.0) * 5000;
  25.     corrente = ((mV - ACSoffset) / mVperAmp);
  26.     #rospy.loginfo("Recebi: " % (corrente))
  27.  
  28. def pd_calc():
  29.     global setpoint, termo_pd_escalonado
  30.     Kp = 5.0
  31.     Kd = 20.0
  32.     ultimo_erro = 0
  33.     erro = setpoint - angulo
  34.     delta_erro = erro - ultimo_erro
  35.     termo_pd = (Kp * erro) + (Kd * delta_erro)
  36.     #termo_pd = constrain(termo_pd, -255, 255);
  37.     termo_pd_escalonado = abs(termo_pd);
  38.     ultimo_erro = erro;
  39.  
  40. def constrain(x,a,b):
  41.     if x < a:
  42.         x = a
  43.     elif x > b:
  44.         x = b
  45.     else:
  46.         x = x
  47.     return x
  48.  
  49. if __name__=='__main__':
  50.     rospy.init_node('no')
  51.    
  52.     sub_p = rospy.Subscriber('posicao', Float64, posi_callback)
  53.     sub_c = rospy.Subscriber('corrente', Float64, corr_callback)
  54.     pub_ctr = rospy.Publisher('controle', Float64, queue_size = 10)
  55.     pub_set = rospy.Publisher('setpt', Float64, queue_size = 10)
  56.     rate = rospy.Rate(50)
  57.  
  58.     while not rospy.is_shutdown():        
  59.         pd_calc()
  60.  
  61.         str = "%s"%termo_pd_escalonado
  62.         rospy.loginfo(str)
  63.  
  64.         pub_ctr.publish(termo_pd_escalonado)
  65.         pub_set.publish(setpoint)
  66.         rate.sleep()
Advertisement
Add Comment
Please, Sign In to add comment