-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made a new node that converts single-float representing compass angle…
… into a quaternion.
- Loading branch information
1 parent
5043fee
commit d7779de
Showing
5 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import rclpy | ||
from rclpy.node import Node | ||
from std_msgs.msg import Float64 | ||
from geometry_msgs.msg import Quaternion | ||
from constants import COMPASS_TOPIC, QOS | ||
import math | ||
|
||
class QuaternionPublisher(Node): | ||
def __init__(self): | ||
super().__init__('quaternion_publisher') | ||
|
||
self.create_subscription(Float64, COMPASS_TOPIC, self.float_callback, 10) | ||
|
||
self.publisher_ = self.create_publisher(Quaternion, 'quaternion_topic', 10) | ||
self.timer = self.create_timer(1.0, self.timer_callback) | ||
|
||
def float_callback(self, msg): | ||
#convert the float to a quaternion | ||
quaternion = self.convert_float_to_quaternion(msg.data) | ||
|
||
self.publisher_.publish(quaternion) | ||
|
||
self.get_logger().info(f'Publishing Quaternion: "{quaternion}"') | ||
|
||
def convert_float_to_quaternion(self, angle): | ||
angle = math.radians(angle) | ||
q = Quaternion() | ||
q.x = 0.0 | ||
q.y = 0.0 | ||
q.z = math.sin(angle/2.0) | ||
q.w = 1-(q.z*q.z) | ||
return q | ||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
try: | ||
rclpy.spin(QuaternionPublisher()) | ||
except KeyboardInterrupt: | ||
print("Shutting down") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Float64 x; | ||
Float64 y; | ||
Float64 z; | ||
Float64 a; |