![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Starter code for brushless motor controller
Diff: main.cpp
- Revision:
- 11:5ff18183764a
- Parent:
- 10:a4b5723b6c9d
- Child:
- 12:6ad981ef8cc9
diff -r a4b5723b6c9d -r 5ff18183764a main.cpp --- a/main.cpp Tue Feb 26 12:23:17 2019 +0000 +++ b/main.cpp Wed Feb 19 21:01:00 2020 +0000 @@ -23,6 +23,11 @@ #define MCSPpin A1 #define MCSNpin A0 +//Test outputs +#define TP0pin D4 +#define TP1pin D13 +#define TP2pin A2 + //Mapping from sequential drive states to motor phase outputs /* State L1 L2 L3 @@ -61,6 +66,9 @@ DigitalOut L3L(L3Lpin); DigitalOut L3H(L3Hpin); +DigitalOut TP1(TP1pin); +PwmOut MotorPWM(PWMpin); + //Set a given drive state void motorOut(int8_t driveState){ @@ -105,6 +113,10 @@ int8_t intState = 0; int8_t intStateOld = 0; + const int32_t PWM_PRD = 2500; + MotorPWM.period_us(PWM_PRD); + MotorPWM.pulsewidth_us(PWM_PRD); + //Initialise the serial port Serial pc(SERIAL_TX, SERIAL_RX); pc.printf("Hello\n\r"); @@ -114,13 +126,15 @@ pc.printf("Rotor origin: %x\n\r",orState); //orState is subtracted from future rotor state inputs to align rotor and motor states + MotorPWM.pulsewidth_us(PWM_PRD/2); //Poll the rotor state and set the motor outputs accordingly to spin the motor while (1) { intState = readRotorState(); if (intState != intStateOld) { + motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive + if (intState == 4 && intStateOld == 3) TP1 = !TP1; intStateOld = intState; - motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive - //pc.printf("%d\n\r",intState); + pc.printf("%d\n\r",intState); } } }