Skip to content

Commit 05d352b

Browse files
committed
kinematic pose fix, preliminary rotate
1 parent bb78c3c commit 05d352b

File tree

5 files changed

+95
-50
lines changed

5 files changed

+95
-50
lines changed

examples/kinematics/kinematics.ino

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ void loop() {
3131
if (millis()-tmotor>20){
3232
tmotor=millis();
3333
alvik.updateMotors();
34-
alvik.kinematics->updatePose(alvik.motor_control_left->getTravel(), alvik.motor_control_right->getTravel());
34+
alvik.kinematics->inverse(alvik.motor_control_left->getRPM(),alvik.motor_control_right->getRPM());
35+
alvik.kinematics->updatePose();
3536
Serial.print("\t");
36-
Serial.print(alvik.kinematics->getDeltaX());
37+
Serial.print(alvik.kinematics->getLinearVelocity());
3738
Serial.print("\t");
38-
Serial.print(alvik.kinematics->getDeltaY());
39+
Serial.print(alvik.kinematics->getAngularVelocity());
40+
Serial.print("\t");
41+
Serial.print(alvik.kinematics->getX());
42+
Serial.print("\t");
43+
Serial.print(alvik.kinematics->getY());
3944
Serial.print("\t");
4045
Serial.print(alvik.kinematics->getTheta());
4146
Serial.print("\n");
@@ -45,16 +50,16 @@ void loop() {
4550
ttask=millis();
4651
switch (task){
4752
case 0:
48-
alvik.drive(50,0);
53+
alvik.rotate(90);
4954
break;
5055
case 1:
51-
alvik.drive(0,0);
56+
alvik.drive(40,0);
5257
break;
5358
case 2:
54-
alvik.drive(0,-90);
59+
alvik.rotate(-90);
5560
break;
5661
case 3:
57-
alvik.drive(0,0);
62+
alvik.drive(-40,0);
5863
break;
5964
}
6065
task++;

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name=Arduino Alvik Carrier
1+
name=Arduino_AlvikCarrier
22
version=0.1.0
33
author=Arduino, Giovanni di Dio Bruno, Lucio Rossi
44
maintainer=Arduino <[email protected]>

src/Arduino_AlvikCarrier.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,22 +610,23 @@ void Arduino_AlvikCarrier::drive(const float linear, const float angular){
610610

611611
void Arduino_AlvikCarrier::rotate(const float angle){
612612
float initial_angle=kinematics->getTheta();
613-
float error=angle-initial_angle;
613+
float final_angle=angle+initial_angle;
614+
float error=angle;
614615
unsigned long t=millis();
615616
while(abs(error)>2){
616617
if (millis()-t>20){
617618
t=millis();
618619
updateMotors();
619-
kinematics->updatePose(motor_control_left->getAngle(),motor_control_right->getAngle());
620-
error=angle-kinematics->getTheta();
620+
kinematics->inverse(motor_control_left->getRPM(),motor_control_right->getRPM());
621+
kinematics->updatePose();
622+
error=final_angle-kinematics->getTheta();
621623
Serial.println(error);
622624
}
623625
if (error>0){
624-
drive(0,40);
626+
drive(0,60);
625627
}else{
626-
drive(0,-40);
628+
drive(0,-60);
627629
}
628-
629630
}
630631
drive(0,0);
631632
updateMotors();

src/Arduino_AlvikCarrier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Arduino_AlvikCarrier{
197197
// Kinematics
198198
void drive(const float linear, const float angular); // set mm/s and deg/s of the robot
199199

200-
void rotate(const float angle);
200+
void rotate(const float angle); // set degrees
201201

202202

203203

src/robotics/kinematics.h

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)