-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot_ros2.py
55 lines (46 loc) · 1.9 KB
/
rot_ros2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Description : A Node in ROS2 named "servo_publisher" that publilshes the current servo angle and rotates the servo continiously in a given range %
# Author: Farrukh Aijaz #
from gpiozero import Servo
from gpiozero.pins.pigpio import PiGPIOFactory
import math
from time import sleep
import rclpy
from std_msgs.msg import Float64
MIN_PULSE_WIDTH = 500 / 1_000_000 # For MG996R
MAX_PULSE_WIDTH = 2500 / 1_000_000 # For MG996R
SERVO_PIN = 12
ROTATION_SPEED = 0.1 # Adjust the rotation speed as needed
def main():
rclpy.init()
node = rclpy.create_node('servo_publisher')
factory = PiGPIOFactory()
servo = Servo(SERVO_PIN, min_pulse_width=MIN_PULSE_WIDTH, max_pulse_width=MAX_PULSE_WIDTH, pin_factory=factory)
publisher = node.create_publisher(Float64, 'servo_angle', 10)
try:
while rclpy.ok():
# Rotate from -15 to 15 degrees
for ver_angle in range(-15, 16):
servo.value = math.sin(math.radians(ver_angle))
sleep(ROTATION_SPEED)
# Publish current servo angle
angle_msg = Float64()
angle_msg.data = float(ver_angle)
publisher.publish(angle_msg)
node.get_logger().info(f'Servo angle: {ver_angle} degrees')
# Rotate back from 15 to -15 degrees
for ver_angle in range(15, -16, -1):
servo.value = math.sin(math.radians(ver_angle))
sleep(ROTATION_SPEED)
# Publish current servo angle
angle_msg = Float64()
angle_msg.data = float(ver_angle)
publisher.publish(angle_msg)
node.get_logger().info(f'Servo angle: {ver_angle} degrees')
except KeyboardInterrupt:
pass
finally:
servo.detach() # Stop the servo
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()