diff_drive.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Includes
12 *****************************************************************************/
13 
14 #include "../../include/xbot_driver/modules/diff_drive.hpp"
15 
16 /*****************************************************************************
17 ** Namespaces
18 *****************************************************************************/
19 
20 namespace xbot {
21 
22 /*****************************************************************************
23 ** Implementation
24 *****************************************************************************/
26  : last_velocity_left(0.0),
27  last_velocity_right(0.0),
28  last_tick_left(0),
29  last_tick_right(0),
30  last_rad_left(0.0),
31  last_rad_right(0.0),
32  // v(0.0), w(0.0), // command velocities, in [m/s] and [rad/s]
33  angular_velocity(0.0),
34  linear_velocity(0.0), // command velocities, in [mm] and [mm/s]
35  point_velocity(2, 0.0), // command velocities, in [m/s] and [rad/s]
36  bias(0.46), // wheelbase, wheel_to_wheel, in [m]
37  wheel_radius(0.0935), // radius of main wheel, in [m]
38  tick_to_rad(
39  3.1415926f * 2 /
40  20000), //减速比1:16的为 32000tick/loop;1:10的为20000tick/loop
41  diff_drive_kinematics(bias, wheel_radius) {}
42 
54 void DiffDrive::update(const unsigned int &time_stamp,
55  const uint16_t &left_encoder,
56  const uint16_t &right_encoder,
57  ecl::Pose2D<double> &pose_update,
58  ecl::linear_algebra::Vector3d &pose_update_rates) {
59  state_mutex.lock();
60  static bool init_l = false;
61  static bool init_r = false;
62  float left_diff_ticks = 0.0f;
63  float right_diff_ticks = 0.0f;
64  unsigned short curr_tick_left = 0;
65  unsigned short curr_tick_right = 0;
66  // unsigned short tmp_diff_time = 0;
67  unsigned int curr_timestamp = 0;
68  curr_timestamp = time_stamp;
69  curr_tick_left = left_encoder;
70  if (!init_l) {
71  last_tick_left = curr_tick_left;
72  init_l = true;
73  }
74  left_diff_ticks = (float)(short)((curr_tick_left - last_tick_left) & 0xffff);
75  last_tick_left = curr_tick_left;
76  last_rad_left -= tick_to_rad * left_diff_ticks;
77 
78  curr_tick_right = right_encoder;
79  if (!init_r) {
80  last_tick_right = curr_tick_right;
81  init_r = true;
82  }
83  right_diff_ticks =
84  (float)(short)((curr_tick_right - last_tick_right) & 0xffff);
85  last_tick_right = curr_tick_right;
86  last_rad_right -= tick_to_rad * right_diff_ticks;
87 
88  // TODO this line and the last statements are really ugly; refactor, put in
89  // another place
90  pose_update =
91  diff_drive_kinematics.forward((double)(tick_to_rad * left_diff_ticks),
92  (double)(tick_to_rad * right_diff_ticks));
93  // std::cout<<"1"<<pose_update<<std::endl;
94 
95  if (curr_timestamp != last_timestamp) {
97  ((double)(short)((curr_timestamp - last_timestamp) & 0xffff)) /
98  1000000.0f;
99  std::cout << "diff_time: " << last_diff_time << std::endl;
100  last_timestamp = curr_timestamp;
101  last_velocity_left = (tick_to_rad * left_diff_ticks) / last_diff_time;
102  last_velocity_right = (tick_to_rad * right_diff_ticks) / last_diff_time;
103  } else {
104  // we need to set the last_velocity_xxx to zero?
105  }
106 
107  pose_update_rates << pose_update.x() / last_diff_time,
108  pose_update.y() / last_diff_time, pose_update.heading() / last_diff_time;
109  // std::cout<<"2"<<pose_update_rates<<std::endl;
110  state_mutex.unlock();
111 }
112 
114  state_mutex.lock();
115  last_rad_left = 0.0;
116  last_rad_right = 0.0;
117  last_velocity_left = 0.0;
118  last_velocity_right = 0.0;
119  state_mutex.unlock();
120 }
121 
122 void DiffDrive::getWheelJointStates(float &wheel_left_angle,
123  float &wheel_left_angle_rate,
124  float &wheel_right_angle,
125  float &wheel_right_angle_rate) {
126  state_mutex.lock();
127  wheel_left_angle = last_rad_left;
128  wheel_right_angle = last_rad_right;
129  wheel_left_angle_rate = last_velocity_left;
130  wheel_right_angle_rate = last_velocity_right;
131  state_mutex.unlock();
132 }
133 
134 void DiffDrive::setVelocityCommands(const float &vx, const float &wz) {
135  // vx: in m/s
136  // wz: in rad/s
137  std::vector<float> cmd_vel;
138  cmd_vel.push_back(vx);
139  cmd_vel.push_back(wz);
140  point_velocity = cmd_vel;
141 }
142 
143 void DiffDrive::velocityCommands(const float &vx, const float &wz) {
144  // vx: in m/s
145  // wz: in rad/s
146  velocity_mutex.lock();
147  linear_velocity = vx;
148  angular_velocity = wz;
149  velocity_mutex.unlock();
150  return;
151 }
152 
153 std::vector<float> DiffDrive::pointVelocity() const { return point_velocity; }
154 
155 std::vector<float> DiffDrive::velocityCommands() {
156  velocity_mutex.lock();
157  std::vector<float> cmd(2);
158  cmd[0] = linear_velocity; // In [m/s]
159  cmd[1] = angular_velocity * 180 / ecl::pi; // In [degree/s]
160  // cmd[1] = angular_velocity; // In [rad/s]
161  velocity_mutex.unlock();
162  return cmd;
163 }
164 
165 short DiffDrive::bound(const float &value) {
166  if (value > static_cast<float>(SHRT_MAX)) return SHRT_MAX;
167  if (value < static_cast<float>(SHRT_MIN)) return SHRT_MIN;
168  return static_cast<short>(value);
169 }
170 
171 } // namespace xbot
unsigned short last_tick_left
Definition: diff_drive.hpp:69
std::vector< float > point_velocity
Definition: diff_drive.hpp:73
std::vector< float > velocityCommands()
Definition: diff_drive.cpp:155
float angular_velocity
Definition: diff_drive.hpp:74
float last_rad_right
Definition: diff_drive.hpp:70
void update(const unsigned int &time_stamp, const uint16_t &left_encoder, const uint16_t &right_encoder, ecl::Pose2D< double > &pose_update, ecl::linear_algebra::Vector3d &pose_update_rates)
Updates the odometry from firmware stamps and encoders.
Definition: diff_drive.cpp:54
float last_velocity_left
Definition: diff_drive.hpp:66
std::vector< float > pointVelocity() const
Definition: diff_drive.cpp:153
unsigned int last_timestamp
Definition: diff_drive.hpp:65
double const pi
void getWheelJointStates(float &wheel_left_angle, float &wheel_left_angle_rate, float &wheel_right_angle, float &wheel_right_angle_rate)
Definition: diff_drive.cpp:122
unsigned short last_tick_right
Definition: diff_drive.hpp:69
ecl::DifferentialDrive::Kinematics diff_drive_kinematics
Definition: diff_drive.hpp:81
float last_diff_time
Definition: diff_drive.hpp:67
EndOfLine endl
float linear_velocity
Definition: diff_drive.hpp:75
void setVelocityCommands(const float &vx, const float &wz)
Definition: diff_drive.cpp:134
short bound(const float &value)
Definition: diff_drive.cpp:165
float last_velocity_right
Definition: diff_drive.hpp:66
double bias
ecl::Mutex state_mutex
Definition: diff_drive.hpp:82
const float tick_to_rad
Definition: diff_drive.hpp:79
ecl::Mutex velocity_mutex
Definition: diff_drive.hpp:82
void f(int i) ecl_debug_throw_decl(StandardException)
Definition: command.hpp:30


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:37