Skip to content

Commit 4a9e2ab

Browse files
Merge pull request #5 from Association-INTech/Asservissement
Asservissement
2 parents 919d6ce + 26016f7 commit 4a9e2ab

2 files changed

Lines changed: 131 additions & 56 deletions

File tree

scripts/masterI2C.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1+
#masteri2c est la pour debug la communication entre l'arduino et la pi.
2+
13
import smbus #type: ignore #ignore the module could not be resolved error because it is a linux only module
24
import time
3-
5+
import numpy as np
6+
import struct
47
# Create an SMBus instance
58
bus = smbus.SMBus(1) # 1 indicates /dev/i2c-1
69

710
# I2C address of the slave
811
SLAVE_ADDRESS = 0x08
912

10-
def write_data(data):
13+
14+
vitesse = 200 # en millimetre par seconde
15+
direction = 100 # en degré
16+
17+
18+
def write_data(vitesse,direction):
1119
# Convert string to list of ASCII values
12-
data_list = [ord(char) for char in data]
13-
bus.write_i2c_block_data(SLAVE_ADDRESS, 0, data_list)
20+
data = struct.pack('<ff', float(vitesse), float(direction))
21+
bus.write_i2c_block_data(SLAVE_ADDRESS, 0, list(data))
1422

15-
<<<<<<< HEAD:scripts/masterI2C.py
1623
import struct
1724

1825
def read_data(length):
@@ -24,23 +31,15 @@ def read_data(length):
2431
return float_value
2532
else:
2633
raise ValueError("Not enough data received from I2C bus")
27-
=======
28-
def read_data(length):
29-
# Read a block of data from the slave
30-
data = bus.read_i2c_block_data(SLAVE_ADDRESS, 0, length)
31-
# Convert list of ASCII values to string
32-
return ''.join(chr(byte) for byte in data)
33-
>>>>>>> origin/oled_annimation:HL/masterI2C.py
3434

3535
if __name__ == "__main__":
36-
try:
37-
# Send data to the slave
38-
write_data("Hello Slave!")
39-
time.sleep(1) # Wait for the slave to process the data
40-
41-
# Request data from the slave
42-
received = read_data(13) # Adjust length as needed
36+
while(True):
37+
vitesse= float(input("vitesse en millimetre par seconde:"))
38+
rotation= float(input("rotation en degré:"))
39+
write_data(vitesse,rotation)
40+
time.sleep(0.1) # Wait for the slave to process the data
41+
received = read_data(8) # Adjust length as needed
4342
print("Received from slave:", received)
4443

45-
except Exception as e:
46-
print("Error:", e)
44+
# Request data from the slave
45+

src/LL/Asserv2bazar/Asserv2bazar.ino

Lines changed: 111 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#define PIN_DIR 10
77
#define PIN_MOT 9
8-
#define PIN_FOURCHE 14
8+
#define PIN_FOURCHE A0
99

1010
Servo moteur;
1111
Servo direction;
@@ -24,21 +24,22 @@ char command;
2424
float Vcons=0;
2525
float old_Vcons ;//consigne
2626
float vitesse=0; //vitesse de la voiture
27-
27+
int dir = 1881;
28+
float dir_recue = 90.0;
2829
//PID
2930
float vieuxEcart=0;
3031
float vieuxTemps=0; //variable utilisee pour mesurer le temps qui passe
3132
float Kp=0.05; //correction prop
32-
float Ki=0.1; //correction integrale
33+
float Ki=0.02; //correction integrale
3334
float Kd=0.; //correction derivee
3435
float integral=0;//valeur de l'integrale dans le PID
3536
float derivee=0; //valeur de la derivee dans le PID
36-
37+
int old_out = 0;
3738
//mesures
3839
volatile int count=0; //variable utilisee pour compter le nombre de fronts montants/descendants
3940
volatile int vieuxCount=0; //stocke l'ancienne valeur de count pour ensuite faire la difference
4041
volatile byte state=LOW;
41-
float mesures[10]; // tableau de mesures pour lisser
42+
float mesures[3]; // tableau de mesures pour lisser
4243

4344
////I2C
4445
union floatToBytes {
@@ -61,6 +62,12 @@ const float r2_NiMh = 1000; // resistance of the second resistor
6162
float voltage_LiPo = 0; // variable to store the value read
6263
float voltage_NiMh = 0; // variable to store the value read
6364

65+
66+
67+
//direction millieu 1851
68+
// tout a gaucge 1231
69+
// tout a droite 2471
70+
6471
float getMeanSpeed(float dt){
6572
int length = sizeof(mesures)/sizeof(mesures[0]);
6673
//ajout d'une nouvelle mesure et suppression de l'ancienne
@@ -94,11 +101,24 @@ float getSpeed(float dt){
94101
vieuxTemps=millis();
95102
return V;
96103
}
104+
105+
97106
void blink(){ //on compte tous les fronts
98107
//Serial.print(count);
99108
count++;
100109
}
110+
111+
101112
float PID(float cons, float mes, float dt) {
113+
114+
115+
if ( old_out <= 0 && cons > 0){ // pour pouvoir sauter directement dans la plage de pwm ou la roue bouge et une transition plus fluide entre marche arriere et avant
116+
integral = 2500; // valeur experimentale
117+
}
118+
else if (old_out >= 0 && cons <0 ){ // pour pouvoir sauter directement dans la plage de pwm où la roue bouge et une transition plus fluide entre marche avant et arriere
119+
integral = -5000; // valeur experimentale
120+
}
121+
102122
// Adjust the measured speed based on the sign of the desired speed
103123
float adjustedMes = (cons < 0) ? -mes : mes;
104124

@@ -112,24 +132,27 @@ float PID(float cons, float mes, float dt) {
112132
integral = integral + e * dt;
113133
float I = Ki * integral;
114134

115-
#if 0
116135
// Derivative term
117136
derivee = (e - vieuxEcart) / dt;
118137
vieuxEcart = e;
119138
float D = Kd * derivee;
120-
#endif
121139

122-
return P + I;
140+
// Garde en mémoir pour passer out à 0 directe lorsque l'on change de sens pour ne pas attendre qu'elle change de sens tout seul (que c'est long mdr
141+
old_out = P + I;
142+
143+
return P + I + D;
123144
}
145+
146+
124147
void calculateVoltage(){
125148
//read from the sensor
126149
// and convert the value to voltage
127150
voltage_LiPo = analogRead(sensorPin_Lipo);
128151
voltage_NiMh = analogRead(sensorPin_NiMh);
129152
voltage_LiPo = voltage_LiPo * (5.0 / 1023.0) * ((r1_LiPo + r2_LiPo) / r1_LiPo);
130153
voltage_NiMh = voltage_NiMh * (5.0 / 1023.0) * ((r1_NiMh + r2_NiMh) / r1_NiMh);
131-
Serial.println(voltage_LiPo);
132-
Serial.println(voltage_NiMh);
154+
//Serial.println(voltage_LiPo);
155+
//Serial.println(voltage_NiMh);
133156
}
134157
void setup() {
135158
Serial.begin(115200);
@@ -138,23 +161,24 @@ void setup() {
138161
moteur.attach(pinMoteur,0,2000);
139162

140163
pinMode(pinDirection,OUTPUT);
141-
direction.attach(pinDirection,0,2000);
164+
direction.attach(pinDirection);
142165

143166
pinMode(pinFourche,INPUT_PULLUP);
144167
enableInterrupt(PIN_FOURCHE, blink, CHANGE); //on regarde a chaque fois que le signal de la fourche change (Montants et Descendants)
145-
146168
moteur.writeMicroseconds(1500);
147169
delay(2000);
148170
moteur.writeMicroseconds(1590);
149171

150172
Wire.begin(8); // Join I2C bus with address #8
151-
Wire.onReceive(receiveEvent); // Register receive event
152-
Wire.onRequest(requestEvent); // Register request event
173+
Wire.onReceive(receiveEvent); // Register receive event
174+
Wire.onRequest(requestEvent); // Register request event
153175
pinMode(13,OUTPUT);
154176

155177
delay(10);
156178
Serial.print("init");
157179
}
180+
181+
158182
void loop() {
159183
calculateVoltage();
160184
// Commandes pour debugger
@@ -175,21 +199,42 @@ void loop() {
175199
case 'q':
176200
Vcons=0;
177201
break;
202+
case 'b':
203+
dir_recue+=10;
204+
break;
205+
case 'n':
206+
dir_recue-=100;
207+
break;
208+
case 'j':
209+
dir_recue+=10;
210+
break;
211+
case 'k':
212+
dir_recue-=10;
213+
break;
178214
}
179-
180215

216+
217+
// Propulsion de la voiture
181218
int deltaT = millis()-vieuxTemps; //temps qui est passé pendant un loop (en millisecondes)
182219
vitesse=getMeanSpeed(deltaT); // on recup la vitesse lissée
183220

184221
int out;
185222

186-
if (Vcons>=0){
223+
if (Vcons == 0){
224+
out = 0;
225+
moteur.writeMicroseconds(out+1500);
226+
Serial.print("out:");
227+
Serial.print(out);
228+
}
229+
else if (Vcons>0){
187230

188231
out = PID(Vcons,vitesse,float(deltaT)/1e3);
232+
189233
moteur.writeMicroseconds(constrain(1500 + out,1500,2000));
190-
191-
} else if ( Vcons<0 && old_Vcons>=0 ){
234+
Serial.print("out:");
235+
Serial.print(constrain(1500 + out,500,2000));
192236

237+
} else if ( Vcons<0 && old_Vcons>=0 ){
193238
out = PID(-8000,vitesse,float(deltaT)/1e3);
194239
moteur.writeMicroseconds(constrain(1500 + out,500,1500));
195240
delay(200);
@@ -205,33 +250,64 @@ void loop() {
205250

206251
old_Vcons = Vcons;
207252

253+
254+
// Direction de la voiture
255+
dir = map(dir_recue,0,180,1231,2471); // remape en degré
256+
direction.writeMicroseconds(dir);
257+
208258
//print debug
209259
#if 1
210-
Serial.print("");
211-
Serial.print(Vcons);
212-
Serial.print(", ");
213-
Serial.print(vitesse);
214-
Serial.print(", ");
215-
Serial.print(Ki);
216-
Serial.print(", ");
217-
Serial.print(Kp);
218-
Serial.print(", ");
219-
Serial.print("out=");
220-
Serial.println(out);
260+
Serial.print(",integrale");
261+
Serial.print(integral);
262+
Serial.print(",const:");
263+
Serial.print(200);
264+
Serial.print(",Vcons:");
265+
Serial.print(Vcons);
266+
Serial.print(",Vitesse:");
267+
Serial.print(vitesse);
268+
Serial.print(",Directino en degré:");
269+
Serial.print(dir_recue);
270+
// Serial.print(", Ki : ");
271+
// Serial.print(Ki);
272+
// Serial.print(", Kp: ");
273+
// Serial.print(Kp);
274+
// Serial.print(", ");
275+
Serial.print(",out2:");
276+
Serial.println(out);
221277
#endif
222278
delay(10);
223-
}
279+
// tout a droite
280+
//delay(1000);
281+
//direction.writeMicroseconds(1761); au millieu
282+
//delay(1000);
283+
//direction.writeMicroseconds(2140);tout a gauche
284+
//delay(1000);
285+
286+
}
224287
void receiveEvent(int byteCount){
225-
for(uint8_t index = 0; index<byteCount; index++){
226-
converter.valueBuffer[index] = Wire.read();
288+
// Ignorer le premier octet "commande" du Raspberry
289+
if (Wire.available()) Wire.read(); // skip cmd byte
290+
291+
if (byteCount >= 9) { // 1 cmd + 8 data
292+
byte buffer[8];
293+
for (int i = 0; i < 8 && Wire.available(); i++) {
294+
buffer[i] = Wire.read();
295+
}
296+
297+
float* vals = (float*)buffer;
298+
Vcons = vals[0]; // reçue en milimetre par secondes
299+
dir_recue = vals[1]; //reçue en degré.
300+
} else {
301+
while (Wire.available()) Wire.read(); // vide le buffer
227302
}
228-
Vcons = converter.valueReading;
229-
230303
}
304+
305+
231306
void requestEvent(){
232307
const int numFloats = 2; // Number of floats to send
233308
float data[numFloats] = {voltage_LiPo, voltage_NiMh}; // Example float values to send
234309
byte* dataBytes = (byte*)data;
310+
235311

236312
Wire.write(dataBytes, sizeof(data));
237-
}
313+
}

0 commit comments

Comments
 (0)