lemueltra

ros_controle_pd

Oct 4th, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 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 = 20.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 = 10.0
  32.     ultimo_erro = 0
  33.     #FREIO NO ARDUINO
  34.     erro = setpoint - angulo
  35.     delta_erro = erro - ultimo_erro
  36.     termo_pd = (Kp * erro) + (Kd * delta_erro)
  37.     termo_pd = constrain(termo_pd, -255, 255);
  38.     termo_pd_escalonado = abs(termo_pd);
  39.     ultimo_erro = erro;
  40.  
  41. def constrain(x,a,b):
  42.     if x < a:
  43.         x = a
  44.     elif x > b:
  45.         x = b
  46.     else:
  47.         x = x
  48.     return x
  49.  
  50. if __name__=='__main__':
  51.     rospy.init_node('no')
  52.    
  53.     sub_p = rospy.Subscriber('posicao', Float64, posi_callback)
  54.     sub_c = rospy.Subscriber('corrente', Float64, corr_callback)
  55.     pub_ctr = rospy.Publisher('controle', Float64, queue_size = 10)
  56.     pub_set = rospy.Publisher('setpt', Float64, queue_size = 10)
  57.     rate = rospy.Rate(50)
  58.  
  59.     while not rospy.is_shutdown():        
  60.         pd_calc()
  61.         pub_ctr.publish(termo_pd_escalonado)
  62.         pub_set.publish(setpoint)
  63.         rate.sleep()
Advertisement
Add Comment
Please, Sign In to add comment