L6230

Dependencies:   L6230_BLDC mbed reScale

Committer:
Marcelocostanzo
Date:
Thu Jun 25 17:44:28 2020 +0000
Revision:
2:d42e168b0fa7
Parent:
1:08226e87671c
Child:
3:95a3a1fbc807
v3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Marcelocostanzo 0:24b2f1c78160 1 #include "mbed.h"
Marcelocostanzo 0:24b2f1c78160 2 #include "L6230_BLDC.h"
Marcelocostanzo 1:08226e87671c 3 #include "reScale.h"
Marcelocostanzo 0:24b2f1c78160 4
Marcelocostanzo 0:24b2f1c78160 5 L6230_BLDC motor(PC_10, PC_11, PC_12, PA_8, PA_9, PA_10);
Marcelocostanzo 0:24b2f1c78160 6
Marcelocostanzo 0:24b2f1c78160 7 AnalogIn feedback_phase_A(PA_0);
Marcelocostanzo 0:24b2f1c78160 8 AnalogIn feedback_phase_B(PC_1);
Marcelocostanzo 0:24b2f1c78160 9 AnalogIn feedback_phase_C(PC_0);
Marcelocostanzo 0:24b2f1c78160 10
Marcelocostanzo 1:08226e87671c 11 AnalogIn pot(PB_1);
Marcelocostanzo 1:08226e87671c 12
Marcelocostanzo 0:24b2f1c78160 13 DigitalOut myled(LED1);
Marcelocostanzo 0:24b2f1c78160 14
Marcelocostanzo 0:24b2f1c78160 15 DigitalIn sw(PC_13);
Marcelocostanzo 0:24b2f1c78160 16
Marcelocostanzo 0:24b2f1c78160 17 Serial pc(USBTX, USBRX);
Marcelocostanzo 0:24b2f1c78160 18
Marcelocostanzo 2:d42e168b0fa7 19 reScale _scale(0, 4095, 800, 5000);
Marcelocostanzo 1:08226e87671c 20
Marcelocostanzo 0:24b2f1c78160 21 float read_current(char);
Marcelocostanzo 0:24b2f1c78160 22
Marcelocostanzo 0:24b2f1c78160 23 int prevStep;
Marcelocostanzo 0:24b2f1c78160 24
Marcelocostanzo 2:d42e168b0fa7 25 long speed;
Marcelocostanzo 2:d42e168b0fa7 26 long adc;
Marcelocostanzo 1:08226e87671c 27
Marcelocostanzo 0:24b2f1c78160 28 int main()
Marcelocostanzo 0:24b2f1c78160 29 {
Marcelocostanzo 0:24b2f1c78160 30 pc.baud(115200);
Marcelocostanzo 0:24b2f1c78160 31
Marcelocostanzo 0:24b2f1c78160 32 pc.printf("Started\r");
Marcelocostanzo 0:24b2f1c78160 33
Marcelocostanzo 0:24b2f1c78160 34 while(1)
Marcelocostanzo 0:24b2f1c78160 35 {
Marcelocostanzo 2:d42e168b0fa7 36 adc = pot.read() * 4096;
Marcelocostanzo 2:d42e168b0fa7 37 speed =_scale.from(adc);
Marcelocostanzo 2:d42e168b0fa7 38 //pc.printf("%i\r",speed);
Marcelocostanzo 1:08226e87671c 39 motor.one_step(1, speed, &prevStep);
Marcelocostanzo 0:24b2f1c78160 40 }
Marcelocostanzo 0:24b2f1c78160 41 }
Marcelocostanzo 0:24b2f1c78160 42
Marcelocostanzo 0:24b2f1c78160 43 float read_current(char PHASE)
Marcelocostanzo 0:24b2f1c78160 44 {
Marcelocostanzo 0:24b2f1c78160 45 float ADC_VALUE;
Marcelocostanzo 0:24b2f1c78160 46
Marcelocostanzo 0:24b2f1c78160 47 switch (PHASE)
Marcelocostanzo 0:24b2f1c78160 48 {
Marcelocostanzo 0:24b2f1c78160 49 case 1: ADC_VALUE = feedback_phase_A.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 50 case 2: ADC_VALUE = feedback_phase_B.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 51 case 3: ADC_VALUE = feedback_phase_C.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 52 }
Marcelocostanzo 0:24b2f1c78160 53
Marcelocostanzo 0:24b2f1c78160 54 float CURRENT = ADC_VALUE / 0.33f; // U = R*I, I = U/R (R = 330mOhm)
Marcelocostanzo 0:24b2f1c78160 55
Marcelocostanzo 0:24b2f1c78160 56 return CURRENT;
Marcelocostanzo 0:24b2f1c78160 57 }