Skip to content

Commit 3853491

Browse files
authored
Merge pull request #43 from UBC-Snowbots/rowzaw/dev
sample packge updates
2 parents 7391c58 + 329af86 commit 3853491

File tree

2 files changed

+74
-34
lines changed

2 files changed

+74
-34
lines changed

src/rover_samples/rv_sample_package/include/sample_node.h

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,47 @@
77
#include <rover_utils/include/roverCommon.h> // roverCommon.h will get you set up with basic ros2 includes as well as our utility libraries. Some parts of the repo
88
#include "sample_node_defines.h"
99
// Other ros2 includes seperated by a line
10-
#include "sensor_msgs/msg/joy.hpp"
10+
#include <sensor_msgs/msg/joy.hpp>
11+
#include <geometry_msgs/msg/twist_stamped.hpp>
1112
// cpp specific or other also seperated by a line
1213
#include <atomic>
1314

1415

1516
// Defines go after includes, but before class definitions
1617
// If you need a lot of defines, create a seperate defines file (see sample_node_defines.h for more info)
17-
#define CONSTANT_ONE 45
18+
#define TIMER_HZ 5
1819
#define MACRO(x) 45*x
20+
#define TIMER_PERIOD_MS (1.0/TIMER_HZ)
1921

2022

2123
// Class definition
2224
class SampleNode : public rclcpp::Node {
2325
public:
2426
SampleNode(); // Constructor is usually defined in .cpp. Its okay if you want to define it here instead.
2527

26-
void send_command(float steering_angle, float speed, float acceleration, float jerk) {
27-
//params can be refreshed at runtime
28-
std::vector<std::string> expected_nodes = this->get_parameter("expected_nodes").as_string_array();
29-
30-
}
31-
32-
void send_gear_command(int gear){
33-
34-
}
35-
3628

3729
private:
30+
// Most member structures and functions are private in a custom ros2 node.
31+
3832
int current_gear = 0;
3933
int prev_paddleR = 0;
4034
int prev_paddleL = 0;
4135

42-
// rclcpp::Publisher<autoware_auto_control_msgs::msg::AckermannControlCommand>::SharedPtr command_publisher_;
43-
// rclcpp::Publisher<autoware_auto_vehicle_msgs::msg::GearCommand>::SharedPtr gear_publisher_;
44-
rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr g29_subscriber_;
4536

37+
// SUBS AND PUBS
38+
rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr cmd_vel_pub;
39+
40+
rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr joy_subscriber;
41+
void joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg); // Sub callback goes underneath subscriber definition
42+
43+
// OTHER ROS2 OBJECTS (Timers)
4644
rclcpp::TimerBase::SharedPtr timer_;
45+
void timer_callback(); // Timer callbacks should go below timer def.
46+
47+
void send_generic_command(float steering_angle, float speed, float acceleration, float jerk);
48+
49+
void send_gear_command(int gear);
50+
51+
4752

48-
void joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg){
49-
float steering_angle = msg->axes[0];
50-
float speed = 99.0 - (msg->axes[3] + 1)*100;
51-
float acceleration = (msg->axes[2] + 1)*20 - (msg->axes[3] + 1)*20;
52-
float jerk = (msg->axes[2] + 1)*20 - (msg->axes[3] + 1)*20;
53-
int paddleR = msg->buttons[4];
54-
int paddleL = msg->buttons[5];
55-
56-
57-
}
58-
};
53+
};

src/rover_samples/rv_sample_package/src/sample_node.cpp

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
#include "sample_node.h"
33

44
// Constructor
5-
SampleNode::SampleNode() : Node("sample_node") { // We create a class using rclcpp::Node as a base class. You can still use another base class if you need, albeit sometimes with difficulties in passing args..
5+
SampleNode::SampleNode() : Node("sample_node")
6+
{ // We create a class using rclcpp::Node as a base class. You can still use another base class if you need, albeit sometimes with difficulties in passing args..
7+
68
// Quality of service example
79
auto qos = rclcpp::QoS(rclcpp::KeepLast(1)).transient_local();
810

9-
double period = 1.0/CONTROL_RATE_HZ;
10-
// timer_ = this->create_wall_timer(
11-
// std::chrono::duration<double>(period),std::bind(&ManualControlNode::test_send, this));
12-
g29_subscriber_ = this->create_subscription<sensor_msgs::msg::Joy>(
11+
joy_subscriber = this->create_subscription<sensor_msgs::msg::Joy>(
1312
"/joy", qos /* or an int for quick qos*/, std::bind(&SampleNode::joy_callback, this, std::placeholders::_1));
14-
13+
14+
cmd_vel_pub = this->create_publisher<geometry_msgs::msg::TwistStamped>("/cmd_vel", qos);
15+
16+
timer_ = this->create_wall_timer(
17+
std::chrono::duration<double>(TIMER_PERIOD_MS),std::bind(&SampleNode::timer_callback, this));
1518

1619
// parameters:
1720
this->declare_parameter("param_name", std::vector<std::string>({"Watchdog"}));
@@ -20,7 +23,8 @@ SampleNode::SampleNode() : Node("sample_node") { // We create a class using rclc
2023

2124

2225
// Main function (entry point of node)
23-
int main(int argc, char *argv[]) {
26+
int main(int argc, char *argv[])
27+
{
2428
// Init ros2 with args.
2529
rclcpp::init(argc, argv);
2630

@@ -33,4 +37,45 @@ int main(int argc, char *argv[]) {
3337
// Code only reaches here if we crash or shutdown the node
3438
rclcpp::shutdown();
3539
return 0;
36-
}
40+
}
41+
42+
// Member function implementations (sub and timer callbacks go first)
43+
44+
void SampleNode::joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg)
45+
{
46+
float steering_angle = msg->axes[0];
47+
float speed = 99.0 - (msg->axes[3] + 1)*100;
48+
float acceleration = (msg->axes[2] + 1)*20 - (msg->axes[3] + 1)*20;
49+
float jerk = (msg->axes[2] + 1)*20 - (msg->axes[3] + 1)*20;
50+
int paddleR = msg->buttons[4];
51+
int paddleL = msg->buttons[5];
52+
send_generic_command(steering_angle, speed, acceleration, jerk);
53+
54+
}
55+
56+
void SampleNode::timer_callback()
57+
{
58+
std::cout << "Node is running (std cout)" << std::endl; // Standard C++ is always available
59+
RCLCPP_INFO(this->get_logger(), "Node is running (ros2 info)"); // ROS2 (rclcpp) also has functions/macros that can replace some of the standard c++ workflow.
60+
RCLCPP_WARN(this->get_logger(), "Node is running (ros2 warn)"); // ROS2 (rclcpp) also has functions that can replace some of the standard c++ workflow.
61+
RCLCPP_ERROR(this->get_logger(), "Node is running (ros2 error)"); // ROS2 (rclcpp) also has functions that can replace some of the standard c++ workflow.
62+
}
63+
64+
65+
void SampleNode::send_generic_command(float steering_angle, float speed, float acceleration, float jerk)
66+
{
67+
// msg.steering angle = steering angle;
68+
// etc...
69+
// some_publisher.pub(msg);
70+
}
71+
72+
73+
74+
75+
76+
77+
78+
// Other examples/tips
79+
80+
//params can be refreshed at runtime
81+
// std::vector<std::string> expected_nodes = this->get_parameter("expected_nodes").as_string_array();

0 commit comments

Comments
 (0)