eLab Team / Mbed 2 deprecated LaLaBox

Dependencies:   mbed CREALIB

Committer:
fbd38
Date:
Thu Dec 06 18:06:21 2018 +0000
Revision:
25:3d3ebfd0a73f
Parent:
24:59caeaa9f82d
Child:
26:bf432a28d0c6
New project CreaROLL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fbd38 16:8ae70f0b8fca 1 /*
fbd38 25:3d3ebfd0a73f 2 * Rotator program
fbd38 16:8ae70f0b8fca 3 *
fbd38 25:3d3ebfd0a73f 4 * Version 1.0, December 2, 2018
fbd38 25:3d3ebfd0a73f 5 * for Nucleo32: L432
fbd38 25:3d3ebfd0a73f 6 *
fbd38 16:8ae70f0b8fca 7 */
fbd38 25:3d3ebfd0a73f 8
fbd38 25:3d3ebfd0a73f 9 #define DEBUG_MODE 3 // 0= no Debug, 1 = BT only, 2 = PC only, 3 = BT + PC
garphil 14:839ab5f50d40 10 #include "Crealab.h"
garphil 12:60c531df03fd 11 Serial bt_uart(PA_9, PA_10);
garphil 17:04c9b524dd94 12 Serial pc_uart(USBTX, USBRX);
fbd38 16:8ae70f0b8fca 13
garphil 20:cdbc31a89c7f 14 // ---------------- Local global variables --------------
fbd38 25:3d3ebfd0a73f 15 int UICommad=0; // New Command from the UI
fbd38 25:3d3ebfd0a73f 16 int32_t CSpeed=0; // Current Speed
fbd38 25:3d3ebfd0a73f 17 int32_t CMod=0; // Current Mode: 0: Stop; 1: Run; -1: Zero
fbd38 25:3d3ebfd0a73f 18 int BTControl=0; // Control taken by Bluetooth
fbd38 25:3d3ebfd0a73f 19 char ExecCommand; // Command to be execute
garphil 2:050f12806bc5 20 // ---------------- PIN DEFINITIONS ---------------------
garphil 2:050f12806bc5 21 DigitalOut myled(LED1); // Blinking LED
fbd38 25:3d3ebfd0a73f 22 //InterruptIn buttonBox(PB_1);
garphil 20:cdbc31a89c7f 23
garphil 20:cdbc31a89c7f 24 // --- Define the Four PINs & Time of movement used for Motor drive -----
fbd38 25:3d3ebfd0a73f 25 Motor motorRotator(PA_8, PA_11, PB_5, PB_4, 1000);
fbd38 25:3d3ebfd0a73f 26 // --- Define the LEDS
fbd38 25:3d3ebfd0a73f 27 int NoLED; // No LED
fbd38 25:3d3ebfd0a73f 28 DigitalOut LedRouge(PB_7);
fbd38 25:3d3ebfd0a73f 29 DigitalOut LedJaune(PB_0);
fbd38 25:3d3ebfd0a73f 30 DigitalOut LedVerte(PA_12);
fbd38 25:3d3ebfd0a73f 31 // --- Define the Speed Potentiometer
fbd38 25:3d3ebfd0a73f 32 AnalogIn SpeedPlus(PA_7);
fbd38 25:3d3ebfd0a73f 33 AnalogIn SpeedMoins(PA_6);
fbd38 25:3d3ebfd0a73f 34 // --- Define the three mode switch
fbd38 25:3d3ebfd0a73f 35 DigitalIn ControlZero(PA_0, PullUp);
fbd38 25:3d3ebfd0a73f 36 DigitalIn ControlStop(PA_1, PullUp);
fbd38 25:3d3ebfd0a73f 37 DigitalIn ControlRun(PA_3, PullUp);
garphil 20:cdbc31a89c7f 38
fbd38 25:3d3ebfd0a73f 39 // ------------ Process ----------------------------------
fbd38 25:3d3ebfd0a73f 40 Ticker HWUI;
garphil 20:cdbc31a89c7f 41
garphil 20:cdbc31a89c7f 42 void help() // Display list of Commands
garphil 20:cdbc31a89c7f 43 {
garphil 20:cdbc31a89c7f 44 DEBUG("List of commands:\n\r");
garphil 20:cdbc31a89c7f 45 DEBUG(" h --> Help, display list of cammands\n\r");
garphil 20:cdbc31a89c7f 46 }
garphil 2:050f12806bc5 47
garphil 20:cdbc31a89c7f 48 /* Stop all processes */
garphil 20:cdbc31a89c7f 49 void stop_all()
garphil 20:cdbc31a89c7f 50 {
fbd38 25:3d3ebfd0a73f 51 motorRotator.Stop();
fbd38 25:3d3ebfd0a73f 52 }
fbd38 25:3d3ebfd0a73f 53
fbd38 25:3d3ebfd0a73f 54 int32_t LireSpeed()
fbd38 25:3d3ebfd0a73f 55 {
fbd38 25:3d3ebfd0a73f 56 double inp;
fbd38 25:3d3ebfd0a73f 57 double inm;
fbd38 25:3d3ebfd0a73f 58 int32_t TargetSpeed;
fbd38 25:3d3ebfd0a73f 59 inp=(double) SpeedPlus.read();
fbd38 25:3d3ebfd0a73f 60 inm=(double) SpeedMoins.read();
fbd38 25:3d3ebfd0a73f 61 TargetSpeed=int((inp-inm)*15.0);
fbd38 25:3d3ebfd0a73f 62 if (TargetSpeed > 9) TargetSpeed = 9;
fbd38 25:3d3ebfd0a73f 63 if (TargetSpeed < -9) TargetSpeed = -9;
fbd38 25:3d3ebfd0a73f 64 return(TargetSpeed);
fbd38 25:3d3ebfd0a73f 65 }
fbd38 25:3d3ebfd0a73f 66
fbd38 25:3d3ebfd0a73f 67 int32_t LireControl()
fbd38 25:3d3ebfd0a73f 68 {
fbd38 25:3d3ebfd0a73f 69 if (ControlStop==0) return(0);
fbd38 25:3d3ebfd0a73f 70 if (ControlRun==0) return(1);
fbd38 25:3d3ebfd0a73f 71 if (ControlZero==0) return(-1);
fbd38 25:3d3ebfd0a73f 72 return(0);
fbd38 25:3d3ebfd0a73f 73 }
fbd38 25:3d3ebfd0a73f 74
fbd38 25:3d3ebfd0a73f 75 void TestHW()
fbd38 25:3d3ebfd0a73f 76 {
fbd38 25:3d3ebfd0a73f 77 LedRouge=1;
fbd38 25:3d3ebfd0a73f 78 wait(0.5);
fbd38 25:3d3ebfd0a73f 79 LedJaune=1;
fbd38 25:3d3ebfd0a73f 80 wait(0.5);
fbd38 25:3d3ebfd0a73f 81 LedVerte=1;
fbd38 25:3d3ebfd0a73f 82 wait(0.5);
fbd38 25:3d3ebfd0a73f 83 LedRouge=0;
fbd38 25:3d3ebfd0a73f 84 wait(0.5);
fbd38 25:3d3ebfd0a73f 85 LedJaune=0;
fbd38 25:3d3ebfd0a73f 86 wait(0.5);
fbd38 25:3d3ebfd0a73f 87 LedVerte=0;
fbd38 25:3d3ebfd0a73f 88 wait(0.5);
garphil 20:cdbc31a89c7f 89 }
garphil 20:cdbc31a89c7f 90
fbd38 25:3d3ebfd0a73f 91 void HardwareUI()
garphil 20:cdbc31a89c7f 92 {
fbd38 25:3d3ebfd0a73f 93 // Display Motor status with Green and Yellow LEDs
fbd38 25:3d3ebfd0a73f 94 if (!NoLED) {
fbd38 25:3d3ebfd0a73f 95 if (motorRotator.GetState() == Motor_RUN) LedVerte=1;
fbd38 25:3d3ebfd0a73f 96 else if (motorRotator.GetState() == Motor_ZERO) LedVerte = !LedVerte;
fbd38 25:3d3ebfd0a73f 97 else LedVerte=0;
fbd38 25:3d3ebfd0a73f 98 if (motorRotator.direction==CLOCKWISE) LedJaune=1;
fbd38 25:3d3ebfd0a73f 99 else LedJaune=0;
fbd38 25:3d3ebfd0a73f 100 LedRouge=BTControl;
fbd38 25:3d3ebfd0a73f 101 }
garphil 20:cdbc31a89c7f 102 }
garphil 20:cdbc31a89c7f 103
fbd38 25:3d3ebfd0a73f 104 char ComposeCommandFromUI()
fbd38 25:3d3ebfd0a73f 105 {
fbd38 25:3d3ebfd0a73f 106 char NewUICommand='-'; // Null command
fbd38 25:3d3ebfd0a73f 107 if (!BTControl) {
fbd38 25:3d3ebfd0a73f 108 int32_t TSp;
fbd38 25:3d3ebfd0a73f 109 int32_t TMod;
fbd38 25:3d3ebfd0a73f 110 int32_t TSpeed;
fbd38 25:3d3ebfd0a73f 111 int32_t TDir;
fbd38 25:3d3ebfd0a73f 112 TSp=LireSpeed();
fbd38 25:3d3ebfd0a73f 113 if (TSp>0) TDir=CLOCKWISE;
fbd38 25:3d3ebfd0a73f 114 if (TSp<0) TDir=COUNTERCLOCKWISE;
fbd38 25:3d3ebfd0a73f 115 TSpeed=abs(TSp);
fbd38 25:3d3ebfd0a73f 116 TMod=LireControl();
fbd38 25:3d3ebfd0a73f 117 if (CMod!=TMod) {
fbd38 25:3d3ebfd0a73f 118 if (TMod==0) NewUICommand = 'S';
fbd38 25:3d3ebfd0a73f 119 if (TMod==1) {
fbd38 25:3d3ebfd0a73f 120 if (TDir == CLOCKWISE) NewUICommand='R';
fbd38 25:3d3ebfd0a73f 121 else NewUICommand='L';
fbd38 25:3d3ebfd0a73f 122 }
fbd38 25:3d3ebfd0a73f 123 if (TMod==-1) NewUICommand = 'Z';
fbd38 25:3d3ebfd0a73f 124 CMod=TMod;
fbd38 25:3d3ebfd0a73f 125 } else {
fbd38 25:3d3ebfd0a73f 126 if (TSpeed!=CSpeed) {
fbd38 25:3d3ebfd0a73f 127 if (TSpeed==0)NewUICommand = 'S';
fbd38 25:3d3ebfd0a73f 128 else NewUICommand = char('0'+TSpeed);
fbd38 25:3d3ebfd0a73f 129 CSpeed=TSpeed;
fbd38 25:3d3ebfd0a73f 130 }
fbd38 25:3d3ebfd0a73f 131 }
fbd38 25:3d3ebfd0a73f 132 }
fbd38 25:3d3ebfd0a73f 133 return (NewUICommand);
fbd38 25:3d3ebfd0a73f 134 }
fbd38 25:3d3ebfd0a73f 135
fbd38 25:3d3ebfd0a73f 136 /* Message */
fbd38 25:3d3ebfd0a73f 137 void message()
fbd38 25:3d3ebfd0a73f 138 {
fbd38 25:3d3ebfd0a73f 139 DEBUG("-------------------------------------------------------\n\r");
fbd38 25:3d3ebfd0a73f 140 DEBUG("-------------------------------------------------------\n\r");
garphil 20:cdbc31a89c7f 141 }
garphil 5:f62e799558c3 142
fbd38 16:8ae70f0b8fca 143 /* Main Routine */
fbd38 16:8ae70f0b8fca 144 int main()
fbd38 16:8ae70f0b8fca 145 {
garphil 1:ab4c9a0a5374 146 myled = 1; // To see something is alive
garphil 20:cdbc31a89c7f 147 bool flaghelp;
fbd38 25:3d3ebfd0a73f 148 // -- Uncomment to program the HC-06 Module
fbd38 25:3d3ebfd0a73f 149 // DEBUG("AT+NAMECreaRoll_x");
fbd38 25:3d3ebfd0a73f 150 // wait(10);
fbd38 25:3d3ebfd0a73f 151 if (ControlZero==0) NoLED=1;
fbd38 25:3d3ebfd0a73f 152 else {
fbd38 25:3d3ebfd0a73f 153 NoLED=0;
fbd38 25:3d3ebfd0a73f 154 TestHW();
fbd38 25:3d3ebfd0a73f 155 }
fbd38 25:3d3ebfd0a73f 156 DEBUG("-------------------------------------------------------\n\r");
fbd38 25:3d3ebfd0a73f 157 DEBUG("----- CreaRoll version 1.0 -----\n\r");
fbd38 25:3d3ebfd0a73f 158 DEBUG("----- faite par fbd38 le 2 Dec 2018 -----\n\r");
fbd38 25:3d3ebfd0a73f 159 DEBUG("-------------------------------------------------------\n\r");
fbd38 25:3d3ebfd0a73f 160 // DEBUG("SystemCoreClock = %d Hz =\n\r", SystemCoreClock);
fbd38 25:3d3ebfd0a73f 161 // DEBUG("Wait 2s\n\r");
garphil 20:cdbc31a89c7f 162 myled = 0; // Real stuff starts here
fbd38 25:3d3ebfd0a73f 163 motorRotator.setSpeed(20.0); /* Default */
fbd38 25:3d3ebfd0a73f 164 // Install UI
fbd38 25:3d3ebfd0a73f 165 HWUI.attach(&HardwareUI, 0.3);
garphil 20:cdbc31a89c7f 166
garphil 20:cdbc31a89c7f 167 while(1) {
fbd38 25:3d3ebfd0a73f 168 ExecCommand='-'; // Clear The Command List
fbd38 25:3d3ebfd0a73f 169 /* Wait Until some command is in the pipe
fbd38 25:3d3ebfd0a73f 170 * eitheir from BT module or HWUI
fbd38 25:3d3ebfd0a73f 171 */
fbd38 25:3d3ebfd0a73f 172 if (bt_uart.readable()) {
fbd38 25:3d3ebfd0a73f 173 char BTlc; // BT Character to execute
fbd38 25:3d3ebfd0a73f 174 BTlc = bt_uart.getc();
fbd38 25:3d3ebfd0a73f 175 if (BTlc=='!') {
fbd38 25:3d3ebfd0a73f 176 BTControl = 1;
fbd38 25:3d3ebfd0a73f 177 } else {
fbd38 25:3d3ebfd0a73f 178 if (BTlc=='.') {
fbd38 25:3d3ebfd0a73f 179 BTControl=0;
fbd38 25:3d3ebfd0a73f 180 } else if (BTControl) ExecCommand=BTlc;
fbd38 25:3d3ebfd0a73f 181 }
fbd38 25:3d3ebfd0a73f 182 }
fbd38 25:3d3ebfd0a73f 183 // In cas of not controlled by BT, get command from the HW UI
fbd38 25:3d3ebfd0a73f 184 if (!BTControl) {
fbd38 25:3d3ebfd0a73f 185 ExecCommand=ComposeCommandFromUI();
fbd38 25:3d3ebfd0a73f 186 }
fbd38 25:3d3ebfd0a73f 187 // ------ Test according with the receved command
fbd38 25:3d3ebfd0a73f 188 if (ExecCommand!='-') {
fbd38 25:3d3ebfd0a73f 189 DEBUG("Commande Recue: [%c]", ExecCommand);
fbd38 25:3d3ebfd0a73f 190 flaghelp = false;
fbd38 25:3d3ebfd0a73f 191 switch (ExecCommand) {
fbd38 25:3d3ebfd0a73f 192 case 'h':
fbd38 25:3d3ebfd0a73f 193 help();
fbd38 25:3d3ebfd0a73f 194 flaghelp=true;
fbd38 25:3d3ebfd0a73f 195 CASE('R', "Infinite Clockwise", motorRotator.RunInfinite(CLOCKWISE); )
fbd38 25:3d3ebfd0a73f 196 CASE('L', "Infinite Counterclockwise", motorRotator.RunInfinite(COUNTERCLOCKWISE); )
fbd38 25:3d3ebfd0a73f 197 CASE('Z', "Go Back to Zero", motorRotator.SetZero(); )
fbd38 25:3d3ebfd0a73f 198 CASE('z', "Define the Zero", motorRotator.DefineZero(); )
fbd38 25:3d3ebfd0a73f 199 CASE('r', "15deg Clockwise", motorRotator.RunDegrees(CLOCKWISE, (float)15.0); )
fbd38 25:3d3ebfd0a73f 200 CASE('l', "15deg Counterclockwise", motorRotator.RunDegrees(COUNTERCLOCKWISE, (float)15.0); )
fbd38 25:3d3ebfd0a73f 201 CASE('1', "Speed 10m/t", motorRotator.setSpeed(600.0); )
fbd38 25:3d3ebfd0a73f 202 CASE('2', "Speed 2m/t", motorRotator.setSpeed(120.0); )
fbd38 25:3d3ebfd0a73f 203 CASE('3', "Speed 1m/t", motorRotator.setSpeed(60.0); )
fbd38 25:3d3ebfd0a73f 204 CASE('4', "Speed 30s/t", motorRotator.setSpeed(30.0); )
fbd38 25:3d3ebfd0a73f 205 CASE('5', "Speed 20s/t", motorRotator.setSpeed(20.0); )
fbd38 25:3d3ebfd0a73f 206 CASE('6', "Speed 15s/t", motorRotator.setSpeed(15.0); )
fbd38 25:3d3ebfd0a73f 207 CASE('7', "Speed 10s/t",motorRotator.setSpeed(10.0); )
fbd38 25:3d3ebfd0a73f 208 CASE('8', "Speed 7s/t",motorRotator.setSpeed(7.0); )
fbd38 25:3d3ebfd0a73f 209 CASE('9', "Speed 5s/t",motorRotator.setSpeed(5.0); )
fbd38 25:3d3ebfd0a73f 210 CASE('0', "STOP ", stop_all(); )
fbd38 25:3d3ebfd0a73f 211 CASE('S', "STOP ", stop_all(); )
fbd38 25:3d3ebfd0a73f 212 CASE('?', "Mistery", message(); )
garphil 20:cdbc31a89c7f 213
fbd38 25:3d3ebfd0a73f 214 default :
fbd38 25:3d3ebfd0a73f 215 DEBUG("invalid command; use: 'h' for help()");
fbd38 25:3d3ebfd0a73f 216 }
garphil 20:cdbc31a89c7f 217 }
garphil 20:cdbc31a89c7f 218 }
garphil 20:cdbc31a89c7f 219 }