-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDriver_moteur.h
145 lines (114 loc) · 2.93 KB
/
Driver_moteur.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef Moteur_encodeur_H
#define Moteur_encodeur_H
#include <Arduino.h>
#include <stdbool.h>
#include <avr/interrupt.h>
#define DIRECT_MODE 0x00
#define PID_MODE 0x01
#define PWM_MODE 0x02
#define POSITION_ENSLAVEMENT 0x00
#define SPEED_ENSLAVEMENT 0x01
#define ENCODER_POS_DEADBAND 1.6
#define DECELERATION_DISTANCE 6
#define SAMPLING_TIME 40
#define Port1 0x01
#define Port2 0x02
#define Port3 0x03
#define Port4 0x04
typedef struct
{
float P, I, D;
float Setpoint, Output, Integral, differential, last_error;
} PID_internal;
typedef struct
{
uint8_t mode;
uint8_t motionState;
int16_t pulseEncoder;
long pulsePos;
float ratio;
float precision;
int16_t maxRPM;
int16_t currentPwm;
int16_t targetPwm;
int16_t previousPwm;
float currentSpeed;
float targetSpeed;
float previousSpeed;
long currentPos;
long targetPos;
long previousPos;
PID_internal PID_speed;
PID_internal PID_pos;
}Me_Encoder_type;
typedef struct
{
uint8_t pin_A;
uint8_t pin_B;
uint8_t pin_PWM;
uint8_t pin_H1;
uint8_t pin_H2;
} Encoder_port_type;
class MoteurEncodeur
{
public:
//Constructeur
MoteurEncodeur();
MoteurEncodeur(uint8_t slot);
//Lecture des attributs
uint8_t getPortNum(void);
uint8_t getIntNum(void);
uint8_t getPinA(void);
uint8_t getPinB(void);
long getPulsePos(void);
float getCurrentrentSpeed(void);
long getCurrentPos(void);
int16_t getCurrentPwm(void);
//Paramètres du moteur
void setRatio(int16_t RatioValue);
void setMaxRPM(int16_t MaxRPM);
//Opération sur la position du moteur
void setPulse(int16_t pulseValue);
void setPulsePos(long pulse_pos);
void pulsePosPlus(void);
void pulsePosMinus(void);
void updateCurPos(void);
//Opération sur la vitesse du moteur
void setCurrentSpeed(float speed);
void updateSpeed(void);
void runSpeed(float speed);
void setSpeed(float speed);
//Opération sur la pwm du moteur
void setTarPWM(int16_t pwm_value);
void setMotorPwm(int16_t pwm);
void stop();
//Choix du mode de régulation
void setMotionMode(int16_t motionMode);
void pwmMove(void); //Sans PID
void encoderMove(void); // Avec PID
//Régulation en vitesse PID
void setSpeedPid(float p,float i,float d);
int16_t speedWithoutPos(void);
//Régulation en position PID
void setPosPid(float p,float i,float d);
void moveTo(long position,float speed = 100);
int16_t pidPositionToPwm(void);
//Autres
boolean isTarPosReached(void); //Flag de mouvement terminé
void loop(void); //Boucle moteur à appeler pour régulation/mouvement
private:
volatile Me_Encoder_type encodeur;
boolean _Lock_flag;
int8_t _Direction;
uint8_t _PIN_A;
uint8_t _PIN_B;
uint8_t _PIN_PWM;
uint8_t _PIN_H1;
uint8_t _PIN_H2;
uint8_t _IntNum;
uint8_t _Slot;
int16_t _Encoder_output;
long _Last_speed_mesurement_time;
long _Last_position_mesurement_time;
};
#endif