Skip to content

Commit 2665693

Browse files
committed
Fix diagonal SimRobot movement
1 parent 9c1eeef commit 2665693

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/mower_simulation/src/SimRobot.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void SimRobot::Start() {
3131
pose_service_ = nh_.advertiseService("/xbot_positioning/set_robot_pose", &SimRobot::OnSetPose, this);
3232
odometry_pub_ = nh_.advertise<nav_msgs::Odometry>("odom_out", 50);
3333
xbot_absolute_pose_pub_ = nh_.advertise<xbot_msgs::AbsolutePose>("xb_pose_out", 50);
34-
timer_ = nh_.createTimer(ros::Duration(0.1), &SimRobot::SimulationStep, this);
34+
// Keep in sync with DiffDriveService::tick_schedule_
35+
timer_ = nh_.createTimer(ros::Duration(0.02), &SimRobot::SimulationStep, this);
3536
timer_.start();
3637
}
3738

@@ -48,10 +49,8 @@ bool SimRobot::OnSetPose(xbot_positioning::SetPoseSrvRequest& req, xbot_position
4849

4950
void SimRobot::GetTwist(double& vx, double& vr) {
5051
std::lock_guard<std::mutex> lk{state_mutex_};
51-
vx = vx_;
52-
vr = vr_;
53-
vx += linear_speed_noise(generator);
54-
vr += angular_speed_noise(generator);
52+
vx = last_noisy_vx_;
53+
vr = last_noisy_vr_;
5554
}
5655

5756
void SimRobot::ResetEmergency() {
@@ -131,12 +130,19 @@ void SimRobot::SimulationStep(const ros::TimerEvent& te) {
131130
// Update Position if not in emergency mode
132131
if (!emergency_latch_) {
133132
double time_diff_s = (now - last_update_).toSec();
134-
double delta_x = (vx_ * cos(pos_heading_)) * time_diff_s;
135-
double delta_y = (vx_ * sin(pos_heading_)) * time_diff_s;
136-
double delta_th = vr_ * time_diff_s;
137-
pos_x_ += delta_x;
138-
pos_y_ += delta_y;
139-
pos_heading_ += delta_th;
133+
double noisy_vx = vx_ + linear_speed_noise(generator);
134+
double noisy_vr = vr_ + angular_speed_noise(generator);
135+
last_noisy_vx_ = noisy_vx;
136+
last_noisy_vr_ = noisy_vr;
137+
if (fabs(noisy_vr) > 1e-6) {
138+
double r = noisy_vx / noisy_vr;
139+
pos_x_ += r * (sin(pos_heading_ + noisy_vr * time_diff_s) - sin(pos_heading_));
140+
pos_y_ -= r * (cos(pos_heading_ + noisy_vr * time_diff_s) - cos(pos_heading_));
141+
pos_heading_ += noisy_vr * time_diff_s;
142+
} else {
143+
pos_x_ += noisy_vx * cos(pos_heading_) * time_diff_s;
144+
pos_y_ += noisy_vx * sin(pos_heading_) * time_diff_s;
145+
}
140146
pos_heading_ = fmod(pos_heading_, M_PI * 2.0);
141147
if (pos_heading_ < 0) {
142148
pos_heading_ += M_PI * 2.0;

src/mower_simulation/src/SimRobot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class SimRobot {
6565
double pos_y_ = 0;
6666
double pos_heading_ = 0;
6767

68+
// Last noisy twist values (for encoder/IMU readback)
69+
double last_noisy_vx_ = 0;
70+
double last_noisy_vr_ = 0;
71+
6872
// Current Emergency State
6973
bool emergency_active_ = false;
7074
// Latched Emergency

0 commit comments

Comments
 (0)