Skip to content

Commit cbb0160

Browse files
committed
huge improvement on motor control
1 parent e39ce05 commit cbb0160

File tree

4 files changed

+154
-4
lines changed

4 files changed

+154
-4
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
This file is part of the Arduino Alvik library.
3+
Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "Arduino_Robot_Firmware.h"
21+
#include "sensor_line.h"
22+
#include "sensor_tof_matrix.h"
23+
#include "ucPack.h"
24+
25+
Arduino_Robot_Firmware robot;
26+
SensorLine line(EXT_A2,EXT_A1,EXT_A0);
27+
SensorTofMatrix tof(robot.wire, EXT_GPIO3, EXT_GPIO2);
28+
29+
30+
ucPack packeter(200);
31+
32+
uint8_t code;
33+
uint8_t msg_size;
34+
35+
unsigned long tmotor=0;
36+
unsigned long tupdate=0;
37+
unsigned long tsensor=0;
38+
39+
40+
float left, right;
41+
uint8_t leds;
42+
43+
uint8_t sensor_id = 0;
44+
45+
46+
uint8_t pid;
47+
float kp, ki, kd;
48+
49+
50+
float reference=30.0;
51+
float interpolation=0.0;
52+
float trip=0.0;
53+
float iterations = 10.0;
54+
float start_value= 0.0;
55+
float end_value = 0.0;
56+
float step_size = 5.0;
57+
float step= 1.0/iterations;
58+
59+
60+
int c=0;
61+
62+
63+
64+
void setup(){
65+
Serial.begin(115200);
66+
robot.begin();
67+
robot.disableIlluminator();
68+
robot.setLedBuiltin(HIGH);
69+
robot.setKPidLeft(30, 450, 0.0); //120 800 10
70+
tmotor=millis();
71+
tupdate=millis();
72+
73+
}
74+
int i=0;
75+
void loop(){
76+
start_value=interpolation;
77+
if (millis()-tupdate>1000){
78+
tupdate=millis();
79+
//reference=-reference;
80+
switch (c){
81+
case 0:
82+
reference = 0;
83+
break;
84+
case 1:
85+
reference = 30;
86+
break;
87+
case 2:
88+
reference = -30;
89+
break;
90+
case 3:
91+
reference = -60;
92+
break;
93+
case 4:
94+
reference = 80;
95+
break;
96+
case 5:
97+
reference = 10;
98+
break;
99+
}
100+
c++;
101+
if (c>5){
102+
c=0;
103+
}
104+
//start_value=robot.getRpmLeft();
105+
end_value=reference;
106+
trip=0.0;
107+
iterations=abs(end_value-start_value)/step_size;
108+
step=1.0/iterations;
109+
i=0;
110+
}
111+
112+
if (millis()-tmotor>20){
113+
if (i<iterations){
114+
i++;
115+
trip+=step;
116+
interpolation=trip*(end_value-start_value)+start_value;
117+
}
118+
tmotor=millis();
119+
robot.updateMotors();
120+
Serial.print("\t");
121+
Serial.print(reference);
122+
Serial.print("\t");
123+
Serial.print(interpolation);
124+
Serial.print("\t");
125+
Serial.print(robot.getRpmLeft());
126+
Serial.print("\n");
127+
robot.setRpmLeft(interpolation);
128+
}
129+
}

src/motor_control.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ class MotorControl{
127127
return mean/float(MEM_SIZE);
128128
}
129129

130-
void clearMemory(){
130+
void clearMemory(const float reset_value=0.0){
131131
for (i=0; i<MEM_SIZE; i++){
132-
measure_memory[i]=0.0;
132+
measure_memory[i]=reset_value;
133133
}
134134
}
135135

@@ -196,7 +196,26 @@ class MotorControl{
196196
measure = encoder->getCount();
197197
encoder->reset();
198198
measure = measure*conversion_factor;
199+
200+
/* experimental
201+
if (abs(measure)-abs(reference)>5){
202+
clearMemory(reference);
203+
}
204+
end */
205+
206+
199207
addMemory(measure);
208+
209+
/*
210+
if (abs(reference)<1.0){
211+
vel_pid->reset();
212+
motor->setSpeed(0);
213+
clearMemory();
214+
}
215+
216+
*/
217+
218+
200219
measure = meanMemory();
201220

202221
/*
@@ -205,6 +224,9 @@ class MotorControl{
205224
measure = measure*conversion_factor;
206225
*/
207226

227+
228+
229+
208230
vel_pid->update(measure);
209231
motor->setSpeed(vel_pid->getControlOutput());
210232
}

src/pid_controller.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class PidController{
117117
}
118118

119119
void reset(){
120-
ctrl_output=0.0;
121120
error=0.0;
122121
error_sum=0.0;
123122
previous_error=0.0;

src/robot_definitions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const float MOTOR_RATIO = MOTOR_CPR*MOTOR_GEAR_RATIO;
3232
#define MOTOR_KP_RIGHT 120.0
3333
#define MOTOR_KI_RIGHT 300.0
3434
#define MOTOR_KD_RIGHT 1.0
35-
#define MOTOR_CONTROL_PERIOD 0.01
35+
#define MOTOR_CONTROL_PERIOD 0.02
3636

3737

3838
// Sensor fusioning parameters

0 commit comments

Comments
 (0)