- This code combines steering and driving in one ticker - Fault check is in a ticker instead of while loop

Dependencies:   mbed MMA8451Q

Committer:
quarren42
Date:
Fri Sep 10 03:52:04 2021 +0000
Revision:
0:0a6756c7e3ed
Child:
1:c324a2849500
test commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quarren42 0:0a6756c7e3ed 1 #include "mbed.h"
quarren42 0:0a6756c7e3ed 2 #include <iostream>
quarren42 0:0a6756c7e3ed 3 #define BLUETOOTHBAUDRATE 115200 //Communication rate of the micro-controller
quarren42 0:0a6756c7e3ed 4 //to the Bluetooth module
quarren42 0:0a6756c7e3ed 5 #define SERIALTXPORT PTE0 //Tx Pin (Sends information to serial device)
quarren42 0:0a6756c7e3ed 6 #define SERIALRXPORT PTE1 //Rx Pin (Receives information from serial
quarren42 0:0a6756c7e3ed 7
quarren42 0:0a6756c7e3ed 8 Serial pc(USBTX, USBRX); // tx, rx
quarren42 0:0a6756c7e3ed 9 Serial bt(SERIALTXPORT, SERIALRXPORT); //(TX, RX) Serial declaration for new serial
quarren42 0:0a6756c7e3ed 10
quarren42 0:0a6756c7e3ed 11 void btRecevie()
quarren42 0:0a6756c7e3ed 12 {
quarren42 0:0a6756c7e3ed 13 string input = bt.getc();
quarren42 0:0a6756c7e3ed 14
quarren42 0:0a6756c7e3ed 15
quarren42 0:0a6756c7e3ed 16 }
quarren42 0:0a6756c7e3ed 17
quarren42 0:0a6756c7e3ed 18
quarren42 0:0a6756c7e3ed 19
quarren42 0:0a6756c7e3ed 20 AnalogIn pot1(PTB0);
quarren42 0:0a6756c7e3ed 21 PwmOut motorRight(PTB2);
quarren42 0:0a6756c7e3ed 22 PwmOut motorLeft(PTB1);
quarren42 0:0a6756c7e3ed 23 DigitalOut ledRed(LED1);
quarren42 0:0a6756c7e3ed 24
quarren42 0:0a6756c7e3ed 25
quarren42 0:0a6756c7e3ed 26 float freq = 20000;
quarren42 0:0a6756c7e3ed 27 float fullBatScalar = 0.5873; //batt at 12.6v
quarren42 0:0a6756c7e3ed 28 float pot1Voltage;
quarren42 0:0a6756c7e3ed 29 float dutyCycleLeft;
quarren42 0:0a6756c7e3ed 30 float dutyCycleRight;
quarren42 0:0a6756c7e3ed 31
quarren42 0:0a6756c7e3ed 32 int main() {
quarren42 0:0a6756c7e3ed 33 bt.baud(BLUETOOTHBAUDRATE);
quarren42 0:0a6756c7e3ed 34 //Sets the communication rate of the micro-controller to the Bluetooth module.
quarren42 0:0a6756c7e3ed 35 pc.printf("Hello World!\n");
quarren42 0:0a6756c7e3ed 36 bt.printf("Hello World!\n");'
quarren42 0:0a6756c7e3ed 37 bt.attach(&btReceive);
quarren42 0:0a6756c7e3ed 38
quarren42 0:0a6756c7e3ed 39 //prints the string to the Tera-Term Console using the Bluetooth object ‘bt’.
quarren42 0:0a6756c7e3ed 40 while(1) {
quarren42 0:0a6756c7e3ed 41 pot1Voltage = pot1 * 3.3f;
quarren42 0:0a6756c7e3ed 42 //.printf("Pot1 Voltage: ", pot1Voltage);
quarren42 0:0a6756c7e3ed 43 bt.printf("Pot1 Voltage = %1.2f volts\n", pot1Voltage);
quarren42 0:0a6756c7e3ed 44 //at full batt (12.6v) max switching freq is 58.73%
quarren42 0:0a6756c7e3ed 45 //dutyCycleLeft = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 46 //dutyCycleRight = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 47
quarren42 0:0a6756c7e3ed 48 dutyCycleLeft = (0.5f * fullBatScalar);
quarren42 0:0a6756c7e3ed 49 dutyCycleRight = (0.25f * fullBatScalar);
quarren42 0:0a6756c7e3ed 50 bt.printf("Duty Cycle = %1.2f \n", dutyCycleLeft);
quarren42 0:0a6756c7e3ed 51
quarren42 0:0a6756c7e3ed 52
quarren42 0:0a6756c7e3ed 53 motorLeft.period(1/freq);
quarren42 0:0a6756c7e3ed 54 motorRight.period(1/freq);
quarren42 0:0a6756c7e3ed 55 motorLeft.write(dutyCycleLeft);
quarren42 0:0a6756c7e3ed 56 motorRight.write(dutyCycleRight);
quarren42 0:0a6756c7e3ed 57
quarren42 0:0a6756c7e3ed 58 wait(0.1);
quarren42 0:0a6756c7e3ed 59
quarren42 0:0a6756c7e3ed 60 }
quarren42 0:0a6756c7e3ed 61 }