Nalyd1002

project_robotics

May 19th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import rclpy
  4. from rclpy.qos import qos_profile_sensor_data
  5. from hrim_actuator_rotaryservo_msgs.msg import GoalRotaryServo
  6. from time import sleep
  7.  
  8. # -------- #
  9.  
  10. rclpy.init(args=None)
  11.  
  12. # Create Node with name "mara_minimal_publisher"
  13. node = rclpy.create_node("mara_minimal_publisher")
  14.  
  15.  
  16. # Create a publisher on topic "/hrim_actuation_servomotor_000000000001/goal_axis1"
  17. pub = node.create_publisher(
  18. GoalRotaryServo, '/hrim_actuator_rotaryservo_000000000001/goal_axis1', qos_profile=qos_profile_sensor_data)
  19. pub2 = node.create_publisher(
  20. GoalRotaryServo, '/hrim_actuator_rotaryservo_000000000002/goal_axis1', qos_profile=qos_profile_sensor_data)
  21. pub3 = node.create_publisher(
  22. GoalRotaryServo, '/hrim_actuator_rotaryservo_000000000001/goal_axis2', qos_profile=qos_profile_sensor_data)
  23. pub4 = node.create_publisher(
  24. GoalRotaryServo, '/hrim_actuator_rotaryservo_000000000003/goal_axis1', qos_profile=qos_profile_sensor_data)
  25.  
  26. # Create message with the same type as the topic, GoalRotaryServo
  27. msg = GoalRotaryServo()
  28. msg2 = GoalRotaryServo()
  29. msg3 = GoalRotaryServo()
  30. msg4 = GoalRotaryServo()
  31.  
  32.  
  33. # Desired position in degrees
  34. position_deg = 0.
  35. position_deg2 = 0.
  36. position_deg3 = 90.
  37. position_deg4 = 0.
  38.  
  39. delta = 0*10
  40.  
  41.  
  42. #!/usr/bin/env python
  43.  
  44. from geometry_msgs.msg import Twist
  45. import sys, select, os
  46. if os.name == 'nt':
  47. import msvcrt, time
  48. else:
  49. import tty, termios
  50.  
  51. def getKey():
  52. if os.name == 'nt':
  53. timeout = 0.1
  54. startTime = time.time()
  55. while(1):
  56. if msvcrt.kbhit():
  57. if sys.version_info[0] >= 3:
  58. return msvcrt.getch().decode()
  59. else:
  60. return msvcrt.getch()
  61. elif time.time() - startTime > timeout:
  62. return ''
  63.  
  64. tty.setraw(sys.stdin.fileno())
  65. rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
  66. if rlist:
  67. key = sys.stdin.read(1)
  68. else:
  69. key = ''
  70.  
  71. termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
  72. return key
  73.  
  74.  
  75. if __name__=="__main__":
  76. if os.name != 'nt':
  77. settings = termios.tcgetattr(sys.stdin)
  78.  
  79.  
  80. rclpy.create_node('project_key')
  81.  
  82. print("Starting...")
  83.  
  84. # Fill message content
  85. msg.position = position_deg * 3.1416/180 # Position to rads
  86. msg.velocity = 0.4 # Velocity in rads/s
  87. msg.control_type = 4 # Position and velocity control
  88.  
  89. msg2.position = position_deg2 * 3.1416/180 # Position to rads
  90. msg2.velocity = 0.4 # Velocity in rads/s
  91. msg2.control_type = 4 # Position and velocity control
  92.  
  93. msg3.position = position_deg3 * 3.1416/180 # Position to rads
  94. msg3.velocity = 0.4 # Velocity in rads/s
  95. msg3.control_type = 4 # Position and velocity control
  96.  
  97. msg4.position = position_deg4 * 3.1416/180 # Position to rads
  98. msg4.velocity = 0.4 # Velocity in rads/s
  99. msg4.control_type = 4 # Position and velocity control
  100.  
  101. print("ok")
  102.  
  103. # Publish message!
  104. pub.publish(msg)
  105. pub2.publish(msg2)
  106. pub3.publish(msg3)
  107. pub4.publish(msg4)
  108.  
  109. # Spin not really needed here since we don't have callbacks
  110. # rclpy.spin_once(node)
  111.  
  112. # Sleep 1 second per loop
  113. sleep(1.)
  114.  
  115. position_deg += delta
  116. if position_deg > 90.0:
  117. delta = -10
  118. elif position_deg < -90.0:
  119. delta = 10
  120. else:
  121. pass
  122.  
  123.  
  124. while rclpy.ok():
  125. print("Enter key:")
  126. key = 'b'
  127. while key != "a":
  128. key = getKey()
  129.  
  130. if(key=='a'):
  131. print(position_deg)
  132. position_deg += 45
  133.  
  134. if(key=='n'):
  135. break
  136.  
  137. if(key=='n'):
  138. break
  139.  
  140.  
  141. node.destroy_node()
  142. rclpy.shutdown()
  143.  
Advertisement
Add Comment
Please, Sign In to add comment