Skip to content

Commit 70ddb1d

Browse files
committed
roughly update serial driver and hero vehicle
1 parent 7712f29 commit 70ddb1d

File tree

7 files changed

+250
-102
lines changed

7 files changed

+250
-102
lines changed

decision/hero_vehicle/include/hero_vehicle/dbus_interpreter.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ using behavior_interface::msg::Chassis;
2626
class DbusInterpreter
2727
{
2828
public:
29-
DbusInterpreter(double max_vel, double max_omega, double aim_sens, double deadzone);
29+
DbusInterpreter(double max_vel, double max_omega, double aim_sens, double deadzone, double video_link_blank_time);
3030

3131
~DbusInterpreter();
3232

33-
void input(const DbusControl::SharedPtr msg);
33+
void input_dbus(const DbusControl::SharedPtr msg);
3434

35-
void input_key(const KeyMouse::SharedPtr msg);
35+
void input_video_link(const KeyMouse::SharedPtr msg);
3636

3737
Move::SharedPtr get_move() const;
3838
geometry_msgs::msg::Twist get_move_ros2_control() const;
@@ -47,11 +47,19 @@ class DbusInterpreter
4747

4848
private:
4949
bool active;
50+
bool keyboard_active_;
51+
// video link
52+
double kbd_move_x = 0.0, kbd_move_y = 0.0;
53+
rclcpp::Time last_video_link_recv_time;
5054

55+
// dbus (DR16)
5156
double ls_x, ls_y, rs_x, rs_y, wheel;
5257
std::string lsw, rsw;
5358
double mouse_x_ = 0.0, mouse_y_ = 0.0, mouse_z_ = 0.0;
5459
bool left_button_ = false, right_button_ = false;
60+
61+
// shared by dbus keyboard and video link keyboard.
62+
// TODO (if both active, fall back to video link)
5563
bool w_ = false, a_ = false, s_ = false;
5664
bool d_ = false, shift_ = false, ctrl_ = false;
5765
bool q_ = false, e_ = false, r_ = false;
@@ -61,7 +69,7 @@ class DbusInterpreter
6169

6270
rclcpp::Time last_update_time_;
6371

64-
double max_vel, max_omega, max_feed, max_shoot, aim_sens, deadzone;
72+
double max_vel, max_omega, max_feed, max_shoot, aim_sens, deadzone, video_link_blank_time;
6573

6674
std::thread update_thread;
6775

decision/hero_vehicle/src/dbus_interpreter.cpp

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "hero_vehicle/dbus_interpreter.h"
22
#include <cmath>
33

4-
DbusInterpreter::DbusInterpreter(double max_vel, double max_omega, double aim_sens, double deadzone)
5-
: max_vel(max_vel), max_omega(max_omega), aim_sens(aim_sens), deadzone(deadzone)
4+
DbusInterpreter::DbusInterpreter(double max_vel, double max_omega, double aim_sens, double deadzone, double video_link_blank_time)
5+
: max_vel(max_vel), max_omega(max_omega), aim_sens(aim_sens), deadzone(deadzone), video_link_blank_time(video_link_blank_time)
66
{
77
// initialize buttons and axes
88
active = false;
9+
keyboard_active_ = false;
910
ls_x = ls_y = rs_x = rs_y = wheel = 0;
1011
lsw = rsw = "";
1112

@@ -37,7 +38,7 @@ DbusInterpreter::~DbusInterpreter()
3738
if (update_thread.joinable()) update_thread.join();
3839
}
3940

40-
void DbusInterpreter::input(const operation_interface::msg::DbusControl::SharedPtr msg)
41+
void DbusInterpreter::input_dbus(const operation_interface::msg::DbusControl::SharedPtr msg)
4142
{
4243
ls_x = msg->ls_x; apply_deadzone(ls_x); // forward is positive
4344
ls_y = msg->ls_y; apply_deadzone(ls_y); // left is positive
@@ -48,8 +49,9 @@ void DbusInterpreter::input(const operation_interface::msg::DbusControl::SharedP
4849
rsw = msg->rsw;
4950
}
5051

51-
void DbusInterpreter::input_key(const operation_interface::msg::KeyMouse::SharedPtr msg)
52+
void DbusInterpreter::input_video_link(const operation_interface::msg::KeyMouse::SharedPtr msg)
5253
{
54+
keyboard_active_ = msg->active;
5355
w_ = msg->w;
5456
a_ = msg->a;
5557
s_ = msg->s;
@@ -70,13 +72,21 @@ void DbusInterpreter::input_key(const operation_interface::msg::KeyMouse::Shared
7072
right_button_ = msg->right_button;
7173
mouse_x_ = msg->mouse_x;
7274
mouse_y_ = msg->mouse_y;
75+
// Video link will send packets even if no keys are pressed, except option panel(p) is active
76+
if(w_ || s_ || a_ || d_)
77+
last_video_link_recv_time = rclcpp::Clock().now();
7378
}
7479

7580
void DbusInterpreter::update()
7681
{
77-
active = (lsw == "MID");
82+
active = (lsw == "MID") || keyboard_active_;
7883
if (!active)
7984
{
85+
move_->vel_x = 0.0;
86+
move_->vel_y = 0.0;
87+
move_->omega = 0.0;
88+
// printf("Update is not active\n");
89+
// TODO: aim ...
8090
return; // do not update if not active, this prevents yaw and pitch from accumulating in standby
8191
}
8292

@@ -112,22 +122,30 @@ void DbusInterpreter::update()
112122

113123

114124
// TODO: Implement Keyboard Actions
115-
int move_x = 0, move_y = 0;
116-
if(w_) move_x += max_vel;
117-
if(s_) move_x -= max_vel;
118-
move_->vel_x += move_x;
119-
120-
if(a_) move_y += max_vel;
121-
if(d_) move_y -= max_vel;
122-
move_->vel_y += move_y;
123-
124-
aim_->yaw -= mouse_x_ * aim_sens * PERIOD / 200;
125-
aim_->pitch -= mouse_y_ * aim_sens * PERIOD / 200; curb(aim_->pitch, M_PI_4);
126-
if(q_) aim_->yaw += aim_sens * 0.5 * PERIOD / 1000;
127-
if(e_) aim_->yaw -= aim_sens * 0.5 * PERIOD / 1000;
128-
129-
// To ensure that the change take place only once per key press
130125
auto current_time = rclcpp::Clock().now();
126+
if (current_time.seconds() - last_video_link_recv_time.seconds() > video_link_blank_time) {
127+
// Video link timeout
128+
keyboard_active_ = false;
129+
kbd_move_x = 0.0;
130+
kbd_move_y = 0.0;
131+
// printf("Inactive video link\n");
132+
} else {
133+
if(w_) kbd_move_x += deadzone*0.1;
134+
if(s_) kbd_move_x -= deadzone*0.1;
135+
apply_deadzone(kbd_move_x);
136+
move_->vel_x += kbd_move_x*max_vel;
137+
138+
if(a_) kbd_move_y += deadzone*0.1;
139+
if(d_) kbd_move_y -= deadzone*0.1;
140+
apply_deadzone(kbd_move_y);
141+
move_->vel_y += kbd_move_y*max_vel;
142+
143+
aim_->yaw -= mouse_x_ * aim_sens * PERIOD / 200;
144+
aim_->pitch -= mouse_y_ * aim_sens * PERIOD / 200; curb(aim_->pitch, M_PI_4);
145+
if(q_) aim_->yaw += aim_sens * 0.5 * PERIOD / 1000;
146+
if(e_) aim_->yaw -= aim_sens * 0.5 * PERIOD / 1000;
147+
}
148+
// To ensure that the change take place only once per key press
131149

132150
// if(current_time.seconds()-last_update_time_.seconds() > 0.2){
133151
// if(c_ && !last_c_) // TOGGLE CHASSIS MODE
@@ -159,9 +177,10 @@ void DbusInterpreter::update()
159177

160178
void DbusInterpreter::apply_deadzone(double &val)
161179
{
162-
if (val < deadzone && val > -deadzone)
163-
{
164-
val = 0;
180+
if (val > deadzone ){
181+
val = deadzone;
182+
} else if (val < -deadzone){
183+
val = -deadzone;
165184
}
166185
}
167186

decision/hero_vehicle/src/hero_vehicle.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class HeroVehicle : public rclcpp::Node
1515
double max_omega = this->declare_parameter("control.rot_vel", 3.0);
1616
double aim_sens = this->declare_parameter("control.stick_sens", 1.57);
1717
double deadzone = this->declare_parameter("control.deadzone", 0.05);
18+
double video_link_blank_time = this->declare_parameter("control.video_link_blank_time", 0.1);
1819
std::string aim_topic = this->declare_parameter("aim_topic", "aim");
1920
std::string shoot_topic = this->declare_parameter("shoot_topic", "shoot");
2021
std::string chassis_topic = this->declare_parameter("chassis_topic","chassis_cmd");
@@ -23,7 +24,7 @@ class HeroVehicle : public rclcpp::Node
2324
enable_key_mouse_ = this->declare_parameter("enable_key_mouse_", true);
2425
enable_ros2_control_ = this->declare_parameter("enable_ros2_control", false);
2526

26-
interpreter_ = std::make_unique<DbusInterpreter>(max_vel, max_omega, aim_sens, deadzone);
27+
interpreter_ = std::make_unique<DbusInterpreter>(max_vel, max_omega, aim_sens, deadzone, video_link_blank_time);
2728

2829
// pub and sub
2930
if (enable_ros2_control_) {
@@ -40,7 +41,7 @@ class HeroVehicle : public rclcpp::Node
4041
std::bind(&HeroVehicle::dbus_callback, this, std::placeholders::_1));
4142
if(enable_key_mouse_){
4243
key_sub_ = this->create_subscription<operation_interface::msg::KeyMouse>(
43-
"key_mouse", 10,
44+
"video_link_key_mouse", 10,
4445
std::bind(&HeroVehicle::key_callback, this, std::placeholders::_1));
4546
}
4647

@@ -68,12 +69,12 @@ class HeroVehicle : public rclcpp::Node
6869

6970
void dbus_callback(const operation_interface::msg::DbusControl::SharedPtr msg)
7071
{
71-
interpreter_->input(msg);
72+
interpreter_->input_dbus(msg);
7273
}
7374

7475
void key_callback(const operation_interface::msg::KeyMouse::SharedPtr msg)
7576
{
76-
interpreter_->input_key(msg);
77+
interpreter_->input_video_link(msg);
7778
}
7879

7980
void timer_callback()

meta_bringup/config/hero.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,16 @@ hero_vehicle:
119119
stick_sens: 5.00 # rad/s
120120
mouse_sens: 10.0
121121
deadzone: 0.15 # percentage
122+
video_link_blank_time: 0.05 # second
122123

123124
capacitor:
124125
ros__parameters:
125126
capacitor_device: "pacific_spirit"
126127
can_interface: "can_1"
128+
referee_serial:
129+
ros__parameters:
130+
regular_link_port: "/dev/tty_REF"
131+
regular_link_baud: 115200
132+
video_link_port: "/dev/tty_VT02"
133+
video_link_baud: 115200
134+
isDebug: false

meta_bringup/launch/hero.launch.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ def generate_launch_description():
103103
load_controller('omni_chassis_controller'),
104104
load_controller('shoot_controller')
105105
]
106-
106+
referee_system_node = Node(
107+
package='referee_serial',
108+
executable='referee_serial_node',
109+
name='referee_serial',
110+
parameters=[robot_config],
111+
output='both',
112+
emulate_tty=True,
113+
)
107114
dbus_control_node = Node(
108115
package='dbus_control',
109116
executable='dbus_control_node',
@@ -152,9 +159,10 @@ def generate_launch_description():
152159
load_joint_state_broadcaster,
153160
# Load controllers
154161
*register_sequential_loading(load_joint_state_broadcaster, *load_controllers),
155-
dbus_control_node,
162+
# dbus_control_node,
163+
referee_system_node,
156164
# auto_sentry_node,
157165
hero_vehicle_node,
158-
ahrs_launch,
166+
# ahrs_launch,
159167
super_capacitor_node,
160168
])

perception/referee_serial/include/referee_serial/referee_serial.hpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ class RefereeSerial
6060
* Interprets the received data and publishes it to the appropriate topic.
6161
* @note This would call the handle_frame method, and is called by the receive_thread.
6262
*/
63-
void receive();
63+
void regular_link_receive();
64+
void video_link_receive();
6465

6566
/**
6667
* @brief Method to reopen the serial port.
6768
* Called when the serial port is closed.
6869
*/
69-
void reopen_port();
70+
void regular_link_reopen_port();
71+
void video_link_reopen_port();
7072

7173
/**
7274
* @brief Method to handle a frame of data.
@@ -77,7 +79,11 @@ class RefereeSerial
7779
* @param frame_type The type of the frame.
7880
*/
7981
template<typename MSG, typename PARSE>
80-
void handle_frame(const std::vector<uint8_t>& prefix,
82+
void regular_link_handle_frame(const std::vector<uint8_t>& prefix,
83+
typename rclcpp::Publisher<MSG>::SharedPtr pub,
84+
const std::string frame_type);
85+
template<typename MSG, typename PARSE>
86+
void video_link_handle_frame(const std::vector<uint8_t>& prefix,
8187
typename rclcpp::Publisher<MSG>::SharedPtr pub,
8288
const std::string frame_type);
8389

@@ -89,17 +95,20 @@ class RefereeSerial
8995
rclcpp::Publisher<operation_interface::msg::RobotState>::SharedPtr robot_state_pub_;
9096

9197
// Serial port
92-
std::unique_ptr<IoContext> ctx_;
93-
std::unique_ptr<SerialPortConfig> config_;
94-
std::unique_ptr<SerialDriver> serial_driver_;
95-
static std::string dev_name; ///< The path to the serial port.
96-
static constexpr const char* dev_null = "/dev/null";
97-
static constexpr uint32_t baud = 115200;
98+
std::unique_ptr<SerialDriver> regular_link_serial_driver_;
99+
std::unique_ptr<SerialDriver> video_link_serial_driver_;
100+
101+
std::unique_ptr<IoContext> regular_link_ctx_;
102+
std::unique_ptr<IoContext> video_link_ctx_;
103+
98104
static constexpr FlowControl fc = FlowControl::NONE;
99105
static constexpr Parity pt = Parity::NONE;
100106
static constexpr StopBits sb = StopBits::ONE;
101107

102-
std::thread receive_thread; ///< Thread to receive data from the serial port.
108+
std::thread regular_link_receive_thread;
109+
std::thread video_link_receive_thread;
110+
111+
bool isDebug = false;
103112
};
104113

105114
#endif // REFEREE_SERIAL_HPP

0 commit comments

Comments
 (0)