Skip to content

Commit 7391c58

Browse files
authored
Merge pull request #42 from UBC-Snowbots/rowzaw/cleanup
cleaning up armjoy and sample node stuff
2 parents 2760166 + 4848344 commit 7391c58

File tree

6 files changed

+217
-192
lines changed

6 files changed

+217
-192
lines changed

src/arm_control/include/joy_arm_control.h

Lines changed: 21 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,100 +12,34 @@
1212

1313
class ArmJoy : public rclcpp::Node {
1414
public:
15-
ArmJoy() : Node("arm_joy_control") {
16-
auto qos = rclcpp::QoS(rclcpp::KeepLast(1)).transient_local();
17-
//command_publisher_ = this->create_publisher<autoware_auto_control_msgs::msg::AckermannControlCommand>("/control/command/control_cmd", qos);
18-
arm_publisher = this->create_publisher<rover_msgs::msg::ArmCommand>("/arm/command", qos);
19-
20-
joy_vibrator = this->create_publisher<sensor_msgs::msg::JoyFeedback>("/joy/feedback", qos);
21-
22-
// timer_ = this->create_wall_timer(
23-
// std::chrono::duration<double>(period),std::bind(&ManualControlNode::test_send, this));
24-
ps4_subscriber = this->create_subscription<sensor_msgs::msg::Joy>(
25-
"/jorsnyuorasniertsnieirsnyuoy", 10, std::bind(&ArmJoy::joy_callback, this, std::placeholders::_1));
26-
arm_subscriber = this->create_subscription<rover_msgs::msg::ArmCommand>(
27-
"/arm/feedback", 10, std::bind(&ArmJoy::arm_callback, this, std::placeholders::_1));
28-
}
29-
30-
void send_command(float steering_angle, float speed, float acceleration, float jerk) {
31-
32-
}
33-
34-
void send_gear_command(int gear){
35-
36-
}
37-
38-
39-
40-
// void test_send(){
41-
// send_command(0.5, 1.0, 1.0, 0.5);
42-
// // rclcpp::logger
43-
44-
// }
15+
ArmJoy();
4516

4617
private:
47-
48-
int current_gear = 0;
49-
int prev_paddleR = 0;
50-
int prev_paddleL = 0;
51-
52-
struct Axis{
53-
float position = 00.00;
54-
float velocity = 00.00;
55-
bool homed = 0;
56-
};
57-
Axis axes[NUM_JOINTS];
5818

59-
// rclcpp::Publisher<autoware_auto_control_msgs::msg::AckermannControlCommand>::SharedPtr command_publisher_;
60-
rclcpp::Publisher<rover_msgs::msg::ArmCommand>::SharedPtr arm_publisher;
19+
// Pubs
20+
rclcpp::Publisher<rover_msgs::msg::ArmCommand>::SharedPtr arm_publisher;
21+
rclcpp::Publisher<sensor_msgs::msg::JoyFeedback>::SharedPtr joy_vibrator;
6122

62-
rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr ps4_subscriber;
63-
rclcpp::Subscription<rover_msgs::msg::ArmCommand>::SharedPtr arm_subscriber;
23+
// Subs
24+
rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr ps4_subscriber;
25+
rclcpp::Subscription<rover_msgs::msg::ArmCommand>::SharedPtr arm_subscriber;
6426

65-
rclcpp::Publisher<sensor_msgs::msg::JoyFeedback>::SharedPtr joy_vibrator;
27+
// Callbacks
28+
void joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg);
29+
void arm_callback(const rover_msgs::msg::ArmCommand::SharedPtr msg);
6630

31+
// Timers
32+
rclcpp::TimerBase::SharedPtr timer_;
6733

68-
rclcpp::TimerBase::SharedPtr timer_;
6934

70-
void joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg){
71-
rover_msgs::msg::ArmCommand target;
72-
target.positions.resize(NUM_JOINTS);
73-
target.velocities.resize(NUM_JOINTS);
74-
float target_positions[6];
75-
float inputs[6];
76-
77-
inputs[2] = msg->axes[0];
78-
inputs[1] = msg->axes[1];
79-
inputs[0] = msg->axes[5] - msg->axes[2];
80-
inputs[3] = msg->axes[3];
81-
inputs[4] = msg->axes[4];
82-
inputs[5] = msg->axes[6];
83-
84-
if(CONTROL_MODE == POSITION_CONTROL){
85-
target.cmd_type = 'P';
86-
87-
for (int i = 0; i < NUM_JOINTS; i++){
88-
target.positions[i] = axes[i].position + inputs[i] * 10;
89-
}
90-
}else if(CONTROL_MODE == VELOCITY_CONTROL){
91-
target.cmd_type = 'V';
92-
for (int i = 0; i < NUM_JOINTS; i++){
93-
target.velocities[i] = inputs[i] * 100;
94-
}
95-
}
96-
97-
arm_publisher->publish(target);
98-
99-
}
100-
101-
void arm_callback(const rover_msgs::msg::ArmCommand::SharedPtr msg){
102-
103-
for (int i = 0; i < NUM_JOINTS; i++){
104-
axes[i].position = msg->positions[i];
105-
// axes[i].velocity = msg->velocities
106-
107-
}
108-
}
109-
35+
int current_gear = 0;
36+
int prev_paddleR = 0;
37+
int prev_paddleL = 0;
11038

39+
struct Axis{
40+
float position = 00.00;
41+
float velocity = 00.00;
42+
bool homed = 0;
43+
};
44+
Axis axes[NUM_JOINTS];
11145
};
Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
1-
//this is a sample node, with a joy input
21
#include "joy_arm_control.h"
32

3+
// Constructor
4+
ArmJoy::ArmJoy() :
5+
Node("arm_joy_control")
6+
{
7+
auto qos = rclcpp::QoS(rclcpp::KeepLast(1)).transient_local();
8+
arm_publisher = this->create_publisher<rover_msgs::msg::ArmCommand>("/arm/command", qos);
49

10+
joy_vibrator = this->create_publisher<sensor_msgs::msg::JoyFeedback>("/joy/feedback", qos);
511

12+
// timer_ = this->create_wall_timer(
13+
// std::chrono::duration<double>(period),std::bind(&ManualControlNode::test_send, this));
14+
ps4_subscriber = this->create_subscription<sensor_msgs::msg::Joy>(
15+
"/jorsnyuorasniertsnieirsnyuoy", 10, std::bind(&ArmJoy::joy_callback, this, std::placeholders::_1));
16+
arm_subscriber = this->create_subscription<rover_msgs::msg::ArmCommand>(
17+
"/arm/feedback", 10, std::bind(&ArmJoy::arm_callback, this, std::placeholders::_1));
18+
}
19+
20+
// Program Entry Point
621
int main(int argc, char *argv[]) {
722
rclcpp::init(argc, argv);
823
auto node = std::make_shared<ArmJoy>();
9-
10-
// Example: send a command with a steering angle of 0.5 rad and speed of 1.0 m/s
11-
1224

1325
rclcpp::spin(node);
1426

1527
rclcpp::shutdown();
1628
return 0;
29+
}
30+
31+
32+
// Member Functions
33+
34+
void ArmJoy::joy_callback(const sensor_msgs::msg::Joy::SharedPtr msg){
35+
rover_msgs::msg::ArmCommand target;
36+
target.positions.resize(NUM_JOINTS);
37+
target.velocities.resize(NUM_JOINTS);
38+
float target_positions[6];
39+
float inputs[6];
40+
41+
inputs[2] = msg->axes[0];
42+
inputs[1] = msg->axes[1];
43+
inputs[0] = msg->axes[5] - msg->axes[2];
44+
inputs[3] = msg->axes[3];
45+
inputs[4] = msg->axes[4];
46+
inputs[5] = msg->axes[6];
47+
48+
if(CONTROL_MODE == POSITION_CONTROL){
49+
target.cmd_type = 'P';
50+
51+
for (int i = 0; i < NUM_JOINTS; i++){
52+
target.positions[i] = axes[i].position + inputs[i] * 10;
53+
}
54+
}else if(CONTROL_MODE == VELOCITY_CONTROL){
55+
target.cmd_type = 'V';
56+
for (int i = 0; i < NUM_JOINTS; i++){
57+
target.velocities[i] = inputs[i] * 100;
58+
}
59+
}
60+
61+
arm_publisher->publish(target);
62+
63+
}
64+
65+
66+
void ArmJoy::arm_callback(const rover_msgs::msg::ArmCommand::SharedPtr msg){
67+
68+
for (int i = 0; i < NUM_JOINTS; i++){
69+
axes[i].position = msg->positions[i];
70+
// axes[i].velocity = msg->velocities
71+
72+
}
1773
}

src/rover_samples/rv_sample_package/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ find_package(ament_cmake REQUIRED)
1010
find_package(rclcpp REQUIRED)
1111
find_package(joy REQUIRED)
1212
find_package(std_msgs REQUIRED)
13+
find_package(rover_utils REQUIRED)
1314
# uncomment the following section in order to fill in
1415
# further dependencies manually.
1516
# find_package(<dependency> REQUIRED)
1617
include_directories(include/)
1718
#add_executable adds a 'target' for CMake to go in and do it's magic
1819
#The target's name here is 'sample_node', and while the naming doesn't need to match the source files, its good practice to keep them the same as this is what you use with ros2 run
19-
add_executable(sample_node src/sample_node.cpp include/sample_node.h)
20-
ament_target_dependencies(sample_node rclcpp joy)
20+
add_executable(sample_node src/sample_node.cpp include/sample_node.h include/sample_node_defines.h)
21+
ament_target_dependencies(sample_node rclcpp joy rover_utils)
2122

2223

2324

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
1-
#define CONTROL_RATE 60.0
2-
#define GEAR_REVERSE 20
3-
#define GEAR_PARKING 22
4-
#define GEAR_NEUTRAL 1
5-
#define GEAR_1 2 //or drive
6-
#define GEAR_2 3 //guessing this is how autoware deals with manual cars
1+
// A little blurb at the top of your header or .cpp is always welcome!!
2+
//? Best to keep this blurb in header? probably?
3+
4+
// Includes
5+
// If file is not in this directory, use <>
6+
// If file path is reative to this directory, use "".
7+
#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
8+
#include "sample_node_defines.h"
9+
// Other ros2 includes seperated by a line
10+
#include "sensor_msgs/msg/joy.hpp"
11+
// cpp specific or other also seperated by a line
12+
#include <atomic>
13+
14+
15+
// Defines go after includes, but before class definitions
16+
// 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 MACRO(x) 45*x
19+
20+
21+
// Class definition
22+
class SampleNode : public rclcpp::Node {
23+
public:
24+
SampleNode(); // Constructor is usually defined in .cpp. Its okay if you want to define it here instead.
25+
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+
36+
37+
private:
38+
int current_gear = 0;
39+
int prev_paddleR = 0;
40+
int prev_paddleL = 0;
41+
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_;
45+
46+
rclcpp::TimerBase::SharedPtr timer_;
47+
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+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Once your node needs to define a buncha stuff (more than 5 or 6 defines, but use your judgement) move the defines to a seperate file, <main header name>_defines.h
2+
#define CONTROL_RATE_HZ 60.0
3+
#define YOUR_MOMS_MASS_KG 3000 // Defines should have units in the name.
4+
5+
6+
7+
//Sometimes, you want to define a constant with a type. in a #define, the preprocessor is what replaces the defines with the value.
8+
// For a constant to be type enforced by the compiler, the compiler needs to see it.
9+
static constexpr bool ON = true;
10+
static constexpr bool OFF = false;
11+
12+
13+
// Now, we should also talk about enums:
14+
15+
// Classic enum we've had since the 90s
16+
// Defaults to int type, but is not scoped by a class. Usage: REVERSE
17+
enum Gears {
18+
REVERSE,
19+
NEUTRAL,
20+
ONE,
21+
TWO
22+
};
23+
24+
25+
enum class GearsScoped { // Now, to use youd need the scope operator. Usage: GearsScoped::REVERSE
26+
REVERSE,
27+
NEUTRAL,
28+
ONE,
29+
TWO
30+
};
31+
32+
// Can also enforce integer types:
33+
enum class GearsScoped_ : uint8_t {
34+
REVERSE = 0,
35+
NEUTRAL = 1,
36+
ONE, // would = 2
37+
TWO
38+
};
39+
40+
41+
// Now, on the border of enums and defines, you can to scoped static constexprs using namespaces:
42+
namespace CarStuff {
43+
44+
static constexpr char SERIAL_PORT_PATH[] = "/dev/ttyUSB0";
45+
static constexpr uint8_t NUM_WHEELS = 3;
46+
47+
// Nested is fine
48+
namespace Gears {
49+
static constexpr int REVERSE = -1;
50+
static constexpr int NEUTRAL = 0;
51+
static constexpr int ONE = 1;
52+
static constexpr int TWO = 2;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)