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
7580void 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
160178void 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
0 commit comments