Restructure codebase
This commit is contained in:
25
src/drawing_controller/CMakeLists.txt
Normal file
25
src/drawing_controller/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(drawing_controller)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# find dependencies
|
||||
find_package(ament_cmake REQUIRED)
|
||||
# uncomment the following section in order to fill in
|
||||
# further dependencies manually.
|
||||
# find_package(<dependency> REQUIRED)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
# the following line skips the linter which checks for copyrights
|
||||
# uncomment the line when a copyright and license is not present in all source files
|
||||
#set(ament_cmake_copyright_FOUND TRUE)
|
||||
# the following line skips cpplint (only works in a git repo)
|
||||
# uncomment the line when this package is not in a git repo
|
||||
#set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
ament_package()
|
||||
18
src/drawing_controller/package.xml
Normal file
18
src/drawing_controller/package.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>drawing_controller</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="root@todo.todo">root</maintainer>
|
||||
<license>TODO: License declaration</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
110
src/drawing_controller/src/drawing_controller.py
Normal file
110
src/drawing_controller/src/drawing_controller.py
Normal file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Sends motions individually to robot_controller"""
|
||||
|
||||
import rclpy
|
||||
from geometry_msgs.msg import Pose, PoseStamped
|
||||
from rclpy.callback_groups import ReentrantCallbackGroup
|
||||
from rclpy.node import Node
|
||||
from rclpy.qos import QoSProfile
|
||||
from random import uniform as rand
|
||||
import math
|
||||
#from tf2_ros.transformations import quaternion_from_euler
|
||||
import lxml.etree as ET
|
||||
|
||||
|
||||
def quaternion_from_euler(ai, aj, ak):
|
||||
ai /= 2.0
|
||||
aj /= 2.0
|
||||
ak /= 2.0
|
||||
ci = math.cos(ai)
|
||||
si = math.sin(ai)
|
||||
cj = math.cos(aj)
|
||||
sj = math.sin(aj)
|
||||
ck = math.cos(ak)
|
||||
sk = math.sin(ak)
|
||||
cc = ci*ck
|
||||
cs = ci*sk
|
||||
sc = si*ck
|
||||
ss = si*sk
|
||||
|
||||
q = [0,0,0,0]
|
||||
q[0] = cj*sc - sj*cs
|
||||
q[1] = cj*ss + sj*cc
|
||||
q[2] = cj*cs - sj*sc
|
||||
q[3] = cj*cc + sj*ss
|
||||
|
||||
return q
|
||||
|
||||
def translate(val, lmin, lmax, rmin, rmax):
|
||||
lspan = lmax - lmin
|
||||
rspan = rmax - rmin
|
||||
val = float(val - lmin) / float(lspan)
|
||||
return rmin + (val * rspan)
|
||||
|
||||
def map_point_function(x_pixels, y_pixels, xlim_lower, xlim_upper, ylim_lower, ylim_upper):
|
||||
def map_point(xpix,ypix):
|
||||
x = translate(xpix, 0, x_pixels, xlim_lower, xlim_upper)
|
||||
y = translate(ypix, 0, y_pixels, ylim_lower, ylim_upper)
|
||||
return (x,y)
|
||||
return map_point
|
||||
|
||||
|
||||
class DrawingController(Node):
|
||||
def __init__(self):
|
||||
super().__init__('drawing_controller')
|
||||
self.publisher_ = self.create_publisher(PoseStamped, '/target_pose', 10)
|
||||
timer_period = 7.0 # seconds
|
||||
self.timer = self.create_timer(timer_period, self.timer_callback)
|
||||
self.i = 0
|
||||
|
||||
# TODO get dimensions from svg
|
||||
|
||||
#print(p)
|
||||
#print(p.position)
|
||||
#print(p.orientation)
|
||||
xml = ET.parse('svg/test.svg')
|
||||
svg = xml.getroot()
|
||||
self.map_point = map_point_function(float(svg.get('width')), float(svg.get('height')), 0.1, 0.5, -0.2, 0.2)
|
||||
self.points = []
|
||||
for child in svg:
|
||||
if (child.tag == 'line'):
|
||||
self.points.append((float(child.get('x1')), float(child.get('y1'))))
|
||||
self.points.append((float(child.get('x2')), float(child.get('y2'))))
|
||||
|
||||
|
||||
def timer_callback(self):
|
||||
next_point = self.points[self.i]
|
||||
point = self.map_point(float(next_point[0]),float(next_point[1]))
|
||||
p = Pose()
|
||||
p.position.x = point[0]
|
||||
p.position.y = point[1]
|
||||
p.position.z = 0.1
|
||||
q = quaternion_from_euler(0.0, math.pi, 0.0)
|
||||
#p.orientation = q
|
||||
p.orientation.x = q[0]
|
||||
p.orientation.y = q[1]
|
||||
p.orientation.z = q[2]
|
||||
p.orientation.w = q[3]
|
||||
ps = PoseStamped()
|
||||
ps.pose = p
|
||||
#print(ps)
|
||||
self.publisher_.publish(ps)
|
||||
self.get_logger().info('Publishing to /target_pose: "%s"' % p)
|
||||
self.i = (self.i + 1) % len(self.points)
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
|
||||
publisher = PublishTarget()
|
||||
|
||||
rclpy.spin(publisher)
|
||||
|
||||
# Destroy the node explicitly
|
||||
# (optional - otherwise it will be done automatically
|
||||
# when the garbage collector destroys the node object)
|
||||
publisher.destroy_node()
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user