Skip to content

Commit d618685

Browse files
committed
control
1 parent 532acd6 commit d618685

File tree

6 files changed

+150
-14
lines changed

6 files changed

+150
-14
lines changed

Arduino_Robot_Firmware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "encoder.h"
88
#include "rgb_led.h"
99
#include "dcmotor.h"
10+
#include "motor_control.h"
1011

1112

1213

dcmotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DCmotor{
1919

2020
public:
2121
DCmotor(const uint32_t _pinA, const uint32_t _chA, const uint32_t _pinB, const uint32_t _chB,
22-
const bool flip=false,TIM_TypeDef * _tim = TIM2, const uint32_t _frequency=20000){
22+
const bool flip=false, TIM_TypeDef * _tim = TIM2, const uint32_t _frequency=20000){
2323
if (!flip){
2424
pinA=_pinA;
2525
chA=_chA;

examples/motor/motor.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "Arduino_Robot_Firmware.h"
2+
3+
Encoder enc_right(TIM5);
4+
5+
DCmotor motor_right(MOTOR_RIGHT_A,MOTOR_RIGHT_A_CH,MOTOR_RIGHT_B,MOTOR_RIGHT_B_CH);
6+
7+
MotorControl right(&motor_right,0,0,0);
8+
9+
void setup(){
10+
enc_right.begin();
11+
motor_right.begin();
12+
}
13+
14+
void loop(){
15+
right.test();
16+
}

motor_control.h

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#ifndef __MOTOR_CONTROL_H__
2+
#define __MOTOR_CONTROL_H__
3+
4+
#include "Arduino.h"
5+
#include "dcmotor.h"
6+
#include "encoder.h"
7+
#include "robot_definitions.h"
8+
9+
#define CONTROL_LIMIT 4095
10+
11+
class MotorControl{
12+
private:
13+
float kp;
14+
float ki;
15+
float kd;
16+
17+
float travel;
18+
float reference;
19+
20+
float measure;
21+
float error;
22+
23+
float ctrl_p;
24+
float ctrl_i;
25+
float ctrl_d;
26+
float actuation;
27+
28+
DCmotor * motor;
29+
Encoder * encoder;
30+
public:
31+
MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp, const float _ki, const float _kd){
32+
motor = _motor;
33+
encoder = _encoder;
34+
35+
kp = _kp;
36+
ki = _ki;
37+
kd = _kd;
38+
39+
travel=0.0;
40+
reference = 0.0;
41+
42+
measure = 0.0;
43+
error = 0.0;
44+
45+
ctrl_p = 0.0;
46+
ctrl_i = 0.0;
47+
ctrl_d = 0.0;
48+
49+
actuation = 0.0;
50+
}
51+
52+
void begin(){
53+
motor->begin();
54+
encoder->begin();
55+
encoder->reset();
56+
}
57+
58+
bool setReference(const float ref){
59+
if ((ref<MOTOR_LIMIT)&&(ref>-MOTOR_LIMIT)){
60+
reference = ref;
61+
return true;
62+
}
63+
return false;
64+
}
65+
66+
float checkLimits(float value){
67+
if (value>CONTROL_LIMIT){
68+
return CONTROL_LIMIT;
69+
}
70+
if (value<-CONTROL_LIMIT){
71+
return -CONTROL_LIMIT;
72+
}
73+
return value;
74+
}
75+
76+
77+
78+
void test(){
79+
motor->setSpeed(2000);
80+
delay(1000);
81+
motor->setSpeed(-2000);
82+
delay(1000);
83+
84+
}
85+
86+
87+
88+
void update(){
89+
measure = encoder->getCount();
90+
encoder->reset();
91+
error = reference - measure;
92+
93+
ctrl_p = kp * error;
94+
ctrl_i = checkLimits(ctrl_i+ki*error);
95+
ctrl_d = kd * (error-prev_error);
96+
97+
prev_error = error;
98+
99+
actuation = checkLimits(ctrl_p+ctrl_i+ctrl_d);
100+
101+
motor->setSpeed(actuation);
102+
}
103+
104+
105+
};
106+
107+
#endif

pinout_definitions.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44

55
// Right Motor
66
#define ENC_RIGHT_TIMER TIM2
7-
#define ENC_RIGHT_A PA_0
8-
#define ENC_RIGHT_B PA_1
7+
#define ENC_RIGHT_A PA0
8+
#define ENC_RIGHT_B PA1
99
#define MOTOR_RIGHT_A PA2
1010
#define MOTOR_RIGHT_A_CH 3
1111
#define MOTOR_RIGHT_B PA3
1212
#define MOTOR_RIGHT_B_CH 4
1313

1414
// Left Motor
1515
#define ENC_LEFT_TIMER TIM3
16-
#define ENC_LEFT_A PC_6
17-
#define ENC_LEFT_B PC_7
16+
#define ENC_LEFT_A PC6
17+
#define ENC_LEFT_B PC7
1818
#define MOTOR_LEFT_A PA15
1919
#define MOTOR_LEFT_A_CH 1
2020
#define MOTOR_LEFT_B PB3
2121
#define MOTOR_LEFT_B_CH 2
2222

2323

2424
// Leds
25-
#define LED_BUILTIN PC_8
26-
#define LED_1_RED PB_12
27-
#define LED_1_GREEN PB_13
28-
#define LED_1_BLUE PB_14
29-
#define LED_2_RED PC_12
30-
#define LED_2_GREEN PB_4
31-
#define LED_2_BLUE PB_5
25+
#define LED_BUILTIN PC8
26+
#define LED_1_RED PB12
27+
#define LED_1_GREEN PB13
28+
#define LED_1_BLUE PB14
29+
#define LED_2_RED PC12
30+
#define LED_2_GREEN PB4
31+
#define LED_2_BLUE PB5
3232

3333
// Analog RC Servos
34-
#define SERVO_A PC_9
35-
#define SERVO_B PA_8
34+
#define SERVO_A PC9
35+
#define SERVO_B PA8
3636

3737

3838
#endif

robot_definitions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __ROBOT_DEFINITIONS_H__
2+
#define __ROBOT_DEFINITIONS_H__
3+
4+
#define CONTROL_LIMIT 4096 // PWM resolution
5+
6+
#define MOTOR_LIMIT 100.0 // Mechanical RPM limit speed of used motors
7+
#define MOTOR_CPR 12.0 // Resolution of the encoder
8+
#define MOTOR_GEAR_RATIO 150.0 // Gear ratio of the motor
9+
10+
const float MOTOR_RATIO = MOTOR_CPR*MOTOR_GEAR_RATIO;
11+
12+
#endif

0 commit comments

Comments
 (0)