L6230

Dependencies:   L6230_BLDC mbed reScale

Committer:
Marcelocostanzo
Date:
Thu Jun 25 17:41:11 2020 +0000
Revision:
1:08226e87671c
Parent:
0:24b2f1c78160
Child:
2:d42e168b0fa7
v2

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 1:08226e87671c 19 reScale _scale(0, 1, 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 1:08226e87671c 25 float speed;
Marcelocostanzo 1:08226e87671c 26 float 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 1:08226e87671c 36 adc = pot.read();
Marcelocostanzo 1:08226e87671c 37 speed =_scale.from(adc);
Marcelocostanzo 1:08226e87671c 38 motor.one_step(1, speed, &prevStep);
Marcelocostanzo 0:24b2f1c78160 39 }
Marcelocostanzo 0:24b2f1c78160 40 }
Marcelocostanzo 0:24b2f1c78160 41
Marcelocostanzo 0:24b2f1c78160 42 float read_current(char PHASE)
Marcelocostanzo 0:24b2f1c78160 43 {
Marcelocostanzo 0:24b2f1c78160 44 float ADC_VALUE;
Marcelocostanzo 0:24b2f1c78160 45
Marcelocostanzo 0:24b2f1c78160 46 switch (PHASE)
Marcelocostanzo 0:24b2f1c78160 47 {
Marcelocostanzo 0:24b2f1c78160 48 case 1: ADC_VALUE = feedback_phase_A.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 49 case 2: ADC_VALUE = feedback_phase_B.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 50 case 3: ADC_VALUE = feedback_phase_C.read() * 3.3f; break; //convert to volts
Marcelocostanzo 0:24b2f1c78160 51 }
Marcelocostanzo 0:24b2f1c78160 52
Marcelocostanzo 0:24b2f1c78160 53 float CURRENT = ADC_VALUE / 0.33f; // U = R*I, I = U/R (R = 330mOhm)
Marcelocostanzo 0:24b2f1c78160 54
Marcelocostanzo 0:24b2f1c78160 55 return CURRENT;
Marcelocostanzo 0:24b2f1c78160 56 }