L6230
Dependencies: L6230_BLDC mbed reScale
main.cpp@3:95a3a1fbc807, 2020-06-25 (annotated)
- Committer:
- Marcelocostanzo
- Date:
- Thu Jun 25 17:52:14 2020 +0000
- Revision:
- 3:95a3a1fbc807
- Parent:
- 2:d42e168b0fa7
v4
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:95a3a1fbc807 | 28 | bool start = 0; |
Marcelocostanzo | 3:95a3a1fbc807 | 29 | |
Marcelocostanzo | 0:24b2f1c78160 | 30 | int main() |
Marcelocostanzo | 0:24b2f1c78160 | 31 | { |
Marcelocostanzo | 0:24b2f1c78160 | 32 | pc.baud(115200); |
Marcelocostanzo | 0:24b2f1c78160 | 33 | |
Marcelocostanzo | 0:24b2f1c78160 | 34 | pc.printf("Started\r"); |
Marcelocostanzo | 0:24b2f1c78160 | 35 | |
Marcelocostanzo | 0:24b2f1c78160 | 36 | while(1) |
Marcelocostanzo | 0:24b2f1c78160 | 37 | { |
Marcelocostanzo | 3:95a3a1fbc807 | 38 | if((sw == 0) && (start == 0)) |
Marcelocostanzo | 3:95a3a1fbc807 | 39 | { |
Marcelocostanzo | 3:95a3a1fbc807 | 40 | start = 1; |
Marcelocostanzo | 3:95a3a1fbc807 | 41 | wait(1); |
Marcelocostanzo | 3:95a3a1fbc807 | 42 | } |
Marcelocostanzo | 3:95a3a1fbc807 | 43 | |
Marcelocostanzo | 3:95a3a1fbc807 | 44 | if((sw == 0) && (start == 1)) |
Marcelocostanzo | 3:95a3a1fbc807 | 45 | { |
Marcelocostanzo | 3:95a3a1fbc807 | 46 | start = 0; |
Marcelocostanzo | 3:95a3a1fbc807 | 47 | wait(1); |
Marcelocostanzo | 3:95a3a1fbc807 | 48 | } |
Marcelocostanzo | 3:95a3a1fbc807 | 49 | |
Marcelocostanzo | 3:95a3a1fbc807 | 50 | if(start == 1) |
Marcelocostanzo | 3:95a3a1fbc807 | 51 | { |
Marcelocostanzo | 3:95a3a1fbc807 | 52 | adc = pot.read() * 4096; |
Marcelocostanzo | 3:95a3a1fbc807 | 53 | speed =_scale.from(adc); |
Marcelocostanzo | 3:95a3a1fbc807 | 54 | //pc.printf("%i\r",speed); |
Marcelocostanzo | 3:95a3a1fbc807 | 55 | motor.one_step(1, speed, &prevStep); |
Marcelocostanzo | 3:95a3a1fbc807 | 56 | } |
Marcelocostanzo | 3:95a3a1fbc807 | 57 | else |
Marcelocostanzo | 3:95a3a1fbc807 | 58 | { |
Marcelocostanzo | 3:95a3a1fbc807 | 59 | motor.stop(); |
Marcelocostanzo | 3:95a3a1fbc807 | 60 | } |
Marcelocostanzo | 0:24b2f1c78160 | 61 | } |
Marcelocostanzo | 0:24b2f1c78160 | 62 | } |
Marcelocostanzo | 0:24b2f1c78160 | 63 | |
Marcelocostanzo | 0:24b2f1c78160 | 64 | float read_current(char PHASE) |
Marcelocostanzo | 0:24b2f1c78160 | 65 | { |
Marcelocostanzo | 0:24b2f1c78160 | 66 | float ADC_VALUE; |
Marcelocostanzo | 0:24b2f1c78160 | 67 | |
Marcelocostanzo | 0:24b2f1c78160 | 68 | switch (PHASE) |
Marcelocostanzo | 0:24b2f1c78160 | 69 | { |
Marcelocostanzo | 0:24b2f1c78160 | 70 | case 1: ADC_VALUE = feedback_phase_A.read() * 3.3f; break; //convert to volts |
Marcelocostanzo | 0:24b2f1c78160 | 71 | case 2: ADC_VALUE = feedback_phase_B.read() * 3.3f; break; //convert to volts |
Marcelocostanzo | 0:24b2f1c78160 | 72 | case 3: ADC_VALUE = feedback_phase_C.read() * 3.3f; break; //convert to volts |
Marcelocostanzo | 0:24b2f1c78160 | 73 | } |
Marcelocostanzo | 0:24b2f1c78160 | 74 | |
Marcelocostanzo | 0:24b2f1c78160 | 75 | float CURRENT = ADC_VALUE / 0.33f; // U = R*I, I = U/R (R = 330mOhm) |
Marcelocostanzo | 0:24b2f1c78160 | 76 | |
Marcelocostanzo | 0:24b2f1c78160 | 77 | return CURRENT; |
Marcelocostanzo | 0:24b2f1c78160 | 78 | } |