Skip to content

Commit c625f20

Browse files
author
Jens
committed
feat: diff drive maybe
1 parent 6018d79 commit c625f20

File tree

10 files changed

+112
-9
lines changed

10 files changed

+112
-9
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IndentWidth: 4

arc/package.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<version>0.1.0</version>
66
<description>TODO: Package description</description>
77
<author email="[email protected]">Kristoffer Plagborg Bak Sørensen</author>
8-
<author email="[email protected]">Jens Heojgaard Jensen</author>
8+
<author email="[email protected]">Jens Høigaard Jensen</author>
99

1010
<maintainer email="[email protected]">Kristoffer Plagborg Bak Sørensen</maintainer>
11-
<maintainer email="[email protected]">Jens Heojgaard Jensen</maintainer>
11+
<maintainer email="[email protected]">Jens Høigaard Jensen</maintainer>
1212

1313
<license file="../LICENSE">MIT</license>
1414

arc_base/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ endif()
1818
# find dependencies
1919
find_package(ament_cmake REQUIRED)
2020
find_package(arc_msgs REQUIRED)
21+
find_package(rclcpp REQUIRED)
22+
23+
add_executable(twist_to_diff_drive_node src/twist_to_diff_drive_node.cpp)
24+
ament_target_dependencies(twist_to_diff_drive_node rclcpp arc_msgs geometry_msgs)
25+
26+
install(TARGETS
27+
twist_to_diff_drive_node
28+
DESTINATION lib/${PROJECT_NAME}
29+
)
2130

2231
if(BUILD_TESTING)
2332
find_package(ament_lint_auto REQUIRED)

arc_base/package.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<author email="[email protected]">Jens Heojgaard Jensen</author>
99

1010
<maintainer email="[email protected]">Kristoffer Plagborg Bak Sørensen</maintainer>
11-
<maintainer email="[email protected]">Jens Heojgaard Jensen</maintainer>
11+
<maintainer email="[email protected]">Jens Høigaard Jensen</maintainer>
1212

1313
<license file="../LICENSE">MIT</license>
1414

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "arc_msgs/msg/diff_drive.hpp"
2+
#include "geometry_msgs/msg/twist.hpp"
3+
#include "rclcpp/rclcpp.hpp"
4+
#include <cmath>
5+
#include <iostream>
6+
7+
template <typename T> using Publisher = rclcpp::Publisher<T>;
8+
9+
template <typename T> using Subscriber = rclcpp::Subscription<T>;
10+
11+
using TwistMsg = geometry_msgs::msg::Twist;
12+
using DiffDriveMsg = arc_msgs::msg::DiffDrive;
13+
14+
class DifferentialDrive {
15+
private:
16+
double wheel_base_;
17+
double wheel_radius_;
18+
19+
public:
20+
DifferentialDrive(double wheel_base, double wheel_radius)
21+
: wheel_base_{wheel_base}, wheel_radius_{wheel_radius} {}
22+
23+
// Calculate the left and right wheel velocities based on linear and angular
24+
// velocity
25+
auto calculateWheelVelocities(double linear_vel, double angular_vel) const {
26+
return std::pair{
27+
(linear_vel - (angular_vel * wheel_base_ / 2.0)) / wheel_radius_,
28+
(linear_vel + (angular_vel * wheel_base_ / 2.0)) / wheel_radius_};
29+
}
30+
};
31+
32+
class TwistToDiffDriveNode : public rclcpp::Node {
33+
public:
34+
TwistToDiffDriveNode() : Node("twist_to_diff_drive_node") {
35+
// Create a DifferentialDrive object with a wheel base of 224mm and a
36+
// wheel radius of 150mm
37+
diff_drive_model_ = DifferentialDrive(0.224, 0.15);
38+
39+
twist_sub_ = this->create_subscription<TwistMsg>(
40+
"cmd_vel", 10,
41+
std::bind(&TwistToDiffDriveNode::twist_cb, this,
42+
std::placeholders::_1));
43+
diff_drive_pub_ =
44+
this->create_publisher<DiffDriveMsg>("diff_drive", 10);
45+
}
46+
47+
private:
48+
Subscriber<TwistMsg>::SharedPtr twist_sub_;
49+
Publisher<DiffDriveMsg>::SharedPtr diff_drive_pub_;
50+
DifferentialDrive diff_drive_model_;
51+
void twist_cb(TwistMsg msg) {
52+
// Calculate the left and right wheel velocities
53+
auto [left_motor, right_motor] =
54+
diff_drive_model_.calculateWheelVelocities(msg.linear.x,
55+
msg.angular.z);
56+
57+
// Publish the left and right motor velocities
58+
auto diff_drive_msg = DiffDriveMsg();
59+
diff_drive_msg.left_motor = left_motor;
60+
diff_drive_msg.right_motor = right_motor;
61+
diff_drive_pub_->publish(diff_drive_msg);
62+
}
63+
};
64+
65+
int main() {
66+
rclcpp::init(argc, argv);
67+
68+
auto node = std::make_shared<TwistToDiffDriveNode>();
69+
70+
rclcpp::spin(node);
71+
rclcpp::shutdown();
72+
73+
return 0;
74+
}

arc_bringup/package.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<version>0.1.0</version>
66
<description>TODO: Package description</description>
77
<author email="[email protected]">Kristoffer Plagborg Bak Sørensen</author>
8-
<author email="[email protected]">Jens Heojgaard Jensen</author>
8+
<author email="[email protected]">Jens Høigaard Jensen</author>
99

1010
<maintainer email="[email protected]">Kristoffer Plagborg Bak Sørensen</maintainer>
11-
<maintainer email="[email protected]">Jens Heojgaard Jensen</maintainer>
11+
<maintainer email="[email protected]">Jens Høigaard Jensen</maintainer>
1212

1313
<license file="../LICENSE">MIT</license>
1414

arc_msgs/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ endif()
1919
find_package(ament_cmake REQUIRED)
2020
find_package(std_msgs REQUIRED)
2121
find_package(geometry_msgs REQUIRED)
22+
find_package(builtin_interface REQUIRED)
23+
find_package(rosidl_default_generators REQUIERD)
24+
25+
set(MSG_FILES
26+
"msg/DiffDrive.msg"
27+
)
28+
29+
rosidl_generate_interfaces(${PROJECT_NAME}
30+
${MSG_FILES}
31+
DEPENDENCIES builtin_interfaces
32+
)
33+
34+
ament_export_dependencies(rosidl_default_runtime)
2235

2336
if(BUILD_TESTING)
2437
find_package(ament_lint_auto REQUIRED)

arc_msgs/msg/DiffDrive.msg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
float32 left_motor
2+
float32 right_motor

arc_msgs/package.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<version>0.1.0</version>
66
<description>TODO: Package description</description>
77
<author email="[email protected]">Kristoffer Plagborg Bak Sørensen</author>
8-
<author email="[email protected]">Jens Heojgaard Jensen</author>
8+
<author email="[email protected]">Jens Høigaard Jensen</author>
99

1010
<maintainer email="[email protected]">Kristoffer Plagborg Bak Sørensen</maintainer>
11-
<maintainer email="[email protected]">Jens Heojgaard Jensen</maintainer>
11+
<maintainer email="[email protected]">Jens Høigaard Jensen</maintainer>
1212

1313
<license file="../LICENSE">MIT</license>
1414

@@ -24,6 +24,10 @@
2424
<test_depend>ament_lint_auto</test_depend>
2525
<test_depend>ament_lint_common</test_depend>
2626

27+
<build_depend>rosidl_default_generators</build_depend>
28+
<exec_depend>rosidl_default_runtime</exec_depend>
29+
<member_of_group>rosidl_interface_packages</member_of_group>
30+
2731
<export>
2832
<build_type>ament_cmake</build_type>
2933
</export>

arc_teleop/package.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<version>0.1.0</version>
66
<description>TODO: Package description</description>
77
<author email="[email protected]">Kristoffer Plagborg Bak Sørensen</author>
8-
<author email="[email protected]">Jens Heojgaard Jensen</author>
8+
<author email="[email protected]">Jens Høigaard Jensen</author>
99

1010
<maintainer email="[email protected]">Kristoffer Plagborg Bak Sørensen</maintainer>
11-
<maintainer email="[email protected]">Jens Heojgaard Jensen</maintainer>
11+
<maintainer email="[email protected]">Jens Høigaard Jensen</maintainer>
1212

1313
<license file="../LICENSE">MIT</license>
1414

0 commit comments

Comments
 (0)