@@ -24,13 +24,9 @@ class Kinematics{
2424 float wheel_diameter;
2525 float wheel_radius;
2626 float wheel_circumference;
27- float rads_to_rpm;
28- float rpm_to_rads;
29- float rads_to_degs;
30- float degs_to_rads;
31- float rotations_to_rads;
32- float rads_to_rotations;
33- float w;
27+
28+ float v, w;
29+ float left_vel, right_vel;
3430
3531 float theta, delta_theta;
3632 float x, y;
@@ -40,6 +36,38 @@ class Kinematics{
4036 float travel;
4137
4238
39+ float mm_to_m (const float mm){
40+ return mm*0.001 ;
41+ }
42+
43+ float m_to_mm (const float m){
44+ return m*1000.0 ;
45+ }
46+
47+ float rads_to_degs (const float rads){
48+ return rads*180.0 /PI;
49+ }
50+
51+ float degs_to_rads (const float degs){
52+ return degs*PI/180.0 ;
53+ }
54+
55+ float rads_to_rpm (const float rads){
56+ return rads*60.0 /(2.0 *PI);
57+ }
58+
59+ float rpm_to_rads (const float rpm){
60+ return rpm*2.0 *PI/60.0 ;
61+ }
62+
63+ float rads_to_rotations (const float rads){
64+ return rads/(2.0 *PI);
65+ }
66+
67+ float rotations_to_rads (const float rotations){
68+ return rotations*2.0 *PI;
69+ }
70+
4371
4472 public:
4573 Kinematics (const float _wheel_track, const float _wheel_diameter){
@@ -48,19 +76,12 @@ class Kinematics{
4876 right_velocity=0.0 ;
4977 linear_velocity=0.0 ;
5078 angular_velocity=0.0 ;
51- wheel_track=_wheel_track;
52- wheel_diameter=_wheel_diameter;
53- wheel_radius=wheel_diameter/2.0 ;
54- wheel_circumference=wheel_diameter*PI;
5579
56- rads_to_rpm= 60.0 /( 2.0 *PI );
57- rpm_to_rads= 2.0 *PI/ 60.0 ;
80+ wheel_track= mm_to_m (_wheel_track );
81+ wheel_diameter= mm_to_m (_wheel_diameter) ;
5882
59- rads_to_degs=180.0 /PI;
60- degs_to_rads=PI/180.0 ;
61-
62- rads_to_rotations=1 /(2.0 *PI);
63- rotations_to_rads=2.0 *PI;
83+ wheel_radius=wheel_diameter/2.0 ;
84+ wheel_circumference=wheel_diameter*PI;
6485
6586 theta=0.0 ;
6687 x=0.0 ;
@@ -77,51 +98,69 @@ class Kinematics{
7798 }
7899
79100 void forward (const float linear, const float angular){
80- w = angular*degs_to_rads ;
81- left_velocity=( 2 *linear-w*wheel_track)/(wheel_diameter );
82- left_velocity=rads_to_rpm*left_velocity;
83- right_velocity =(2 *linear+ w*wheel_track)/(wheel_diameter);
84- right_velocity=rads_to_rpm*right_velocity ;
101+ v = mm_to_m (linear) ;
102+ w = degs_to_rads (angular );
103+
104+ left_velocity =(2 *v- w*wheel_track)/(wheel_diameter);
105+ right_velocity=( 2 *v+w*wheel_track)/(wheel_diameter) ;
85106 }
86107
87- void inverse (const float left_vel, const float right_vel){
108+ void inverse (const float left, const float right){
109+ left_vel=rpm_to_rads (left);
110+ right_vel=rpm_to_rads (right);
111+
88112 linear_velocity=(left_vel+right_vel)*wheel_radius/2.0 ;
89113 angular_velocity=(-left_vel+right_vel)*wheel_radius/wheel_track;
90114 }
91115
92116 float getLeftVelocity (){
93- return left_velocity;
117+ return rads_to_rpm ( left_velocity) ;
94118 }
95119
96120 float getRightVelocity (){
97- return right_velocity;
121+ return rads_to_rpm ( right_velocity) ;
98122 }
99123
100124 float getLinearVelocity (){
101- return linear_velocity;
125+ return m_to_mm ( linear_velocity) ;
102126 }
103127
104128 float getAngularVelocity (){
105- return angular_velocity;
129+ return rads_to_degs ( angular_velocity) ;
106130 }
107131
132+ /*
108133 void updatePose(const float left_rotation, const float right_rotation){
109134 delta_left=left_rotation*wheel_circumference;
110135 delta_right=right_rotation*wheel_circumference;
111136 delta_travel=(delta_left+delta_right)/2.0;
112- delta_theta=(-delta_left+delta_right)/(2.0 *wheel_track);
137+ delta_theta=(-delta_left+delta_right)/(wheel_track);
138+
113139 delta_x=delta_travel*cos(theta+delta_theta/2.0);
114140 delta_y=delta_travel*sin(theta+delta_theta/2.0);
141+
142+ delta_x=delta_travel*cos(delta_theta);
143+ delta_y=delta_travel*sin(delta_theta);
144+ x+=delta_x;
145+ y+=delta_y;
146+ theta+=delta_theta;
147+ travel+=delta_travel;
148+ }
149+ */
150+
151+ void updatePose (){
152+ delta_theta=angular_velocity*0.02 ;
153+ delta_x=linear_velocity*cos (theta)*0.02 ;
154+ delta_y=linear_velocity*sin (theta)*0.02 ;
115155 x+=delta_x;
116156 y+=delta_y;
117157 theta+=delta_theta;
118- travel+=delta_travel;
119158 }
120159
121160 void resetPose (const float initial_x=0.0 , const float initial_y=0.0 , const float initial_theta=0.0 ){
122- x=initial_x;
123- y=initial_y;
124- theta=degs_to_rads* initial_theta;
161+ x=mm_to_m ( initial_x) ;
162+ y=mm_to_m ( initial_y) ;
163+ theta=degs_to_rads ( initial_theta) ;
125164 travel=0.0 ;
126165 delta_x=0.0 ;
127166 delta_y=0.0 ;
@@ -141,7 +180,7 @@ class Kinematics{
141180 }
142181
143182 float getTheta (){
144- return rads_to_degs* theta;
183+ return rads_to_degs ( theta) ;
145184 }
146185
147186 float getTravel (){
@@ -157,7 +196,7 @@ class Kinematics{
157196 }
158197
159198 float getDeltaTheta (){
160- return rads_to_degs* delta_theta;
199+ return rads_to_degs ( delta_theta) ;
161200 }
162201
163202
0 commit comments