An example code for using the motorshield for 2.74
Dependencies: QEI_pmw MotorShield
main.cpp@3:1eee0d9f1223, 2020-09-12 (annotated)
- Committer:
- elijahsj
- Date:
- Sat Sep 12 17:12:04 2020 +0000
- Revision:
- 3:1eee0d9f1223
- Parent:
- 2:358bdcaaf496
Fixed types
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 0:92642f80db7c | 1 | #include "mbed.h" |
elijahsj | 0:92642f80db7c | 2 | #include "rtos.h" |
elijahsj | 0:92642f80db7c | 3 | #include "QEI.h" |
elijahsj | 0:92642f80db7c | 4 | #include "MotorShield.h" |
elijahsj | 0:92642f80db7c | 5 | #include "HardwareSetup.h" |
elijahsj | 0:92642f80db7c | 6 | |
elijahsj | 0:92642f80db7c | 7 | Serial pc(USBTX, USBRX); // USB Serial Terminal |
elijahsj | 0:92642f80db7c | 8 | Timer t; // Timer to measure elapsed time of experiment |
elijahsj | 0:92642f80db7c | 9 | |
elijahsj | 0:92642f80db7c | 10 | QEI encoderA(PE_9,PE_11, NC, 1200, QEI::X4_ENCODING); // MOTOR A ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 0:92642f80db7c | 11 | QEI encoderB(PA_5, PB_3, NC, 1200, QEI::X4_ENCODING); // MOTOR B ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 0:92642f80db7c | 12 | QEI encoderC(PC_6, PC_7, NC, 1200, QEI::X4_ENCODING); // MOTOR C ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 0:92642f80db7c | 13 | QEI encoderD(PD_12, PD_13, NC, 1200, QEI::X4_ENCODING);// MOTOR D ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 0:92642f80db7c | 14 | |
elijahsj | 0:92642f80db7c | 15 | MotorShield motorShield(12000); //initialize the motor shield with a PWM period of 12000 ticks or ~20kHZ |
elijahsj | 0:92642f80db7c | 16 | |
elijahsj | 0:92642f80db7c | 17 | int main (void) |
elijahsj | 0:92642f80db7c | 18 | { |
elijahsj | 0:92642f80db7c | 19 | // Setup experiment |
elijahsj | 0:92642f80db7c | 20 | t.reset(); |
elijahsj | 0:92642f80db7c | 21 | t.start(); |
elijahsj | 0:92642f80db7c | 22 | encoderA.reset(); |
elijahsj | 3:1eee0d9f1223 | 23 | float position; |
elijahsj | 3:1eee0d9f1223 | 24 | float velocity; |
elijahsj | 3:1eee0d9f1223 | 25 | float current; |
elijahsj | 1:7811196d1a57 | 26 | motorShield.motorAWrite(0, 0); //turn motor A off |
elijahsj | 1:7811196d1a57 | 27 | |
elijahsj | 1:7811196d1a57 | 28 | //use the motor shield as follows: |
elijahsj | 1:7811196d1a57 | 29 | //motorShield.motorAWrite(DUTY CYCLE, DIRECTION), DIRECTION = 0 is forward, DIRECTION =1 is backwards. |
elijahsj | 0:92642f80db7c | 30 | |
elijahsj | 0:92642f80db7c | 31 | // Run experiment for 10 seconds |
elijahsj | 0:92642f80db7c | 32 | while( t.read() < 10 ) { |
elijahsj | 0:92642f80db7c | 33 | // Perform control loop logic |
elijahsj | 0:92642f80db7c | 34 | if (t.read() < 5) |
elijahsj | 0:92642f80db7c | 35 | motorShield.motorAWrite(0.5, 0); //run motor A at 50% duty cycle and in the forward direction for 5 seconds |
elijahsj | 0:92642f80db7c | 36 | else |
elijahsj | 0:92642f80db7c | 37 | motorShield.motorAWrite(0.5, 1); //run motor A at 50% duty cycle and in the reverse direction for 5 seconds |
elijahsj | 0:92642f80db7c | 38 | |
elijahsj | 2:358bdcaaf496 | 39 | position = 0; //MODIFY THIS |
elijahsj | 2:358bdcaaf496 | 40 | velocity = 0; //MODIFY THIS |
elijahsj | 2:358bdcaaf496 | 41 | current = motorShield.readCurrentA()*(30.0/65536.0)-15; //read current for motor A in amps. Note: this is a slightly different current sensor so its a different conversion than last lab. |
elijahsj | 0:92642f80db7c | 42 | |
elijahsj | 3:1eee0d9f1223 | 43 | pc.printf("Current reading: %f Amps, Velocity: %f, Angle: %f \n\r", current, velocity, position); |
elijahsj | 0:92642f80db7c | 44 | |
elijahsj | 1:7811196d1a57 | 45 | wait(.01); //run control loop at 100Hz |
elijahsj | 0:92642f80db7c | 46 | } |
elijahsj | 0:92642f80db7c | 47 | |
elijahsj | 0:92642f80db7c | 48 | motorShield.motorAWrite(0, 0); //turn motor A off |
elijahsj | 0:92642f80db7c | 49 | |
elijahsj | 0:92642f80db7c | 50 | while(1) { |
elijahsj | 0:92642f80db7c | 51 | //loop forever |
elijahsj | 0:92642f80db7c | 52 | } |
elijahsj | 0:92642f80db7c | 53 | |
elijahsj | 0:92642f80db7c | 54 | } // end main |