Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-example-mbed5-blinky by
main.cpp@42:b69538bba4f9, 2017-07-05 (annotated)
- Committer:
- gunarthon
- Date:
- Wed Jul 05 21:05:56 2017 +0000
- Revision:
- 42:b69538bba4f9
- Parent:
- 41:3bc2a3885b9d
- Child:
- 43:5123f24e0b2c
added UDP option (untested)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
gunarthon | 38:b760c09b311c | 2 | #include "Queue.h" |
gunarthon | 38:b760c09b311c | 3 | #include "TextLCD.h" |
gunarthon | 38:b760c09b311c | 4 | #include "pid.h" |
gunarthon | 39:e4f5710b2f31 | 5 | #include <Serial.h> |
gunarthon | 42:b69538bba4f9 | 6 | #include "EthernetInterface.h" |
Jonathan Austin |
0:2757d7abb7d9 | 7 | |
gunarthon | 38:b760c09b311c | 8 | //definitions |
gunarthon | 38:b760c09b311c | 9 | typedef struct { |
gunarthon | 40:19d51f6e6800 | 10 | double input; |
gunarthon | 38:b760c09b311c | 11 | } message_t; |
gunarthon | 38:b760c09b311c | 12 | |
gunarthon | 42:b69538bba4f9 | 13 | bool useUDP = false; |
gunarthon | 40:19d51f6e6800 | 14 | const int heightResolution = 1000; |
gunarthon | 39:e4f5710b2f31 | 15 | const int BAUDRATE = 115200; |
gunarthon | 42:b69538bba4f9 | 16 | const char* host_address = "10.1.1.101"; |
gunarthon | 42:b69538bba4f9 | 17 | const int host_port = 7; |
gunarthon | 40:19d51f6e6800 | 18 | const int cameraFPS = 60; |
gunarthon | 40:19d51f6e6800 | 19 | const int displayRefreshTime = 200; |
gunarthon | 38:b760c09b311c | 20 | const double setPoint = 0.5; |
gunarthon | 40:19d51f6e6800 | 21 | const double Kp = 0.02; |
gunarthon | 40:19d51f6e6800 | 22 | const double Ki = 0; |
gunarthon | 40:19d51f6e6800 | 23 | const double Kd = 0; |
gunarthon | 41:3bc2a3885b9d | 24 | const double configStep = 0.001; |
gunarthon | 41:3bc2a3885b9d | 25 | const double configFastStep = configStep*10; |
gunarthon | 38:b760c09b311c | 26 | |
gunarthon | 40:19d51f6e6800 | 27 | const int simulationSeconds = 10; |
gunarthon | 40:19d51f6e6800 | 28 | |
gunarthon | 40:19d51f6e6800 | 29 | enum {btnRIGHT, btnUP, btnDOWN, btnLEFT, btnSELECT, btnNONE, btnSIZE}; |
gunarthon | 40:19d51f6e6800 | 30 | enum {menuSETPOINT, menuPID, menuSIMULATE, menuSIZE}; |
gunarthon | 40:19d51f6e6800 | 31 | enum {parameterKP, parameterKI, parameterKD, parameterSIZE}; |
gunarthon | 40:19d51f6e6800 | 32 | enum {simulSTEP, simulRAMP, simulNONE, simulSIZE}; |
Jonathan Austin |
0:2757d7abb7d9 | 33 | |
gunarthon | 38:b760c09b311c | 34 | //Pins |
gunarthon | 38:b760c09b311c | 35 | PwmOut outPin(D3); |
gunarthon | 40:19d51f6e6800 | 36 | //DigitalOut led2(LED2); |
gunarthon | 38:b760c09b311c | 37 | DigitalOut led3(LED3); |
gunarthon | 38:b760c09b311c | 38 | AnalogIn buttons(A0); |
gunarthon | 38:b760c09b311c | 39 | |
gunarthon | 38:b760c09b311c | 40 | //Threads |
gunarthon | 41:3bc2a3885b9d | 41 | Thread lcdThread; |
gunarthon | 38:b760c09b311c | 42 | Thread pidThread; |
gunarthon | 42:b69538bba4f9 | 43 | Thread serialThread; |
gunarthon | 38:b760c09b311c | 44 | Thread hmiThread; |
gunarthon | 40:19d51f6e6800 | 45 | Thread setPointThread; |
gunarthon | 42:b69538bba4f9 | 46 | Thread udpThread; |
gunarthon | 38:b760c09b311c | 47 | |
gunarthon | 38:b760c09b311c | 48 | //Global variables |
gunarthon | 38:b760c09b311c | 49 | TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7 |
gunarthon | 38:b760c09b311c | 50 | Pid* pidController; |
gunarthon | 38:b760c09b311c | 51 | MemoryPool<message_t, 16> mpool; |
gunarthon | 38:b760c09b311c | 52 | Queue<message_t,16> messageQueue; |
gunarthon | 38:b760c09b311c | 53 | |
gunarthon | 40:19d51f6e6800 | 54 | //Menu settings |
gunarthon | 40:19d51f6e6800 | 55 | int menu = menuSETPOINT; |
gunarthon | 40:19d51f6e6800 | 56 | int parameter = parameterKP; |
gunarthon | 40:19d51f6e6800 | 57 | int simul = simulNONE; |
gunarthon | 41:3bc2a3885b9d | 58 | bool setPointSimul = false; |
gunarthon | 40:19d51f6e6800 | 59 | |
gunarthon | 42:b69538bba4f9 | 60 | |
gunarthon | 42:b69538bba4f9 | 61 | // Network interface |
gunarthon | 42:b69538bba4f9 | 62 | EthernetInterface net; |
gunarthon | 39:e4f5710b2f31 | 63 | |
gunarthon | 38:b760c09b311c | 64 | //=============================Thread Methodes================================== |
gunarthon | 40:19d51f6e6800 | 65 | void setPointMethode(void) |
gunarthon | 40:19d51f6e6800 | 66 | { |
gunarthon | 41:3bc2a3885b9d | 67 | while(1) //toggle every 20 seconds between 2 setPoint values |
gunarthon | 40:19d51f6e6800 | 68 | { |
gunarthon | 41:3bc2a3885b9d | 69 | while(!setPointSimul) Thread::wait(2000); |
gunarthon | 41:3bc2a3885b9d | 70 | |
gunarthon | 41:3bc2a3885b9d | 71 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 72 | { |
gunarthon | 41:3bc2a3885b9d | 73 | pidController->setSetPoint(0.2); |
gunarthon | 41:3bc2a3885b9d | 74 | Thread::wait(20000); |
gunarthon | 41:3bc2a3885b9d | 75 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 76 | { |
gunarthon | 41:3bc2a3885b9d | 77 | pidController->setSetPoint(0.8); |
gunarthon | 41:3bc2a3885b9d | 78 | Thread::wait(20000); |
gunarthon | 41:3bc2a3885b9d | 79 | } |
gunarthon | 41:3bc2a3885b9d | 80 | } |
gunarthon | 40:19d51f6e6800 | 81 | } |
gunarthon | 40:19d51f6e6800 | 82 | } |
gunarthon | 41:3bc2a3885b9d | 83 | void LcdMethode(void) |
gunarthon | 38:b760c09b311c | 84 | { |
gunarthon | 38:b760c09b311c | 85 | while(true) |
gunarthon | 38:b760c09b311c | 86 | { |
gunarthon | 38:b760c09b311c | 87 | led3 = !led3; |
gunarthon | 40:19d51f6e6800 | 88 | |
gunarthon | 40:19d51f6e6800 | 89 | double lastPwm = pidController->getLastPwm(); |
gunarthon | 40:19d51f6e6800 | 90 | double lastInput = pidController->getLastInput(); |
gunarthon | 40:19d51f6e6800 | 91 | |
gunarthon | 40:19d51f6e6800 | 92 | if(menu == menuSETPOINT) |
gunarthon | 40:19d51f6e6800 | 93 | { |
gunarthon | 40:19d51f6e6800 | 94 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 95 | lcd.printf("set in out"); |
gunarthon | 40:19d51f6e6800 | 96 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 97 | lcd.printf("%d", int(1000*pidController->getSetPoint())); |
gunarthon | 40:19d51f6e6800 | 98 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 99 | lcd.printf("%d", int(1000*lastInput)); |
gunarthon | 40:19d51f6e6800 | 100 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 101 | lcd.printf("%d", int(1000*lastPwm)); |
gunarthon | 40:19d51f6e6800 | 102 | } |
gunarthon | 40:19d51f6e6800 | 103 | else if(menu == menuPID) |
gunarthon | 40:19d51f6e6800 | 104 | { |
gunarthon | 40:19d51f6e6800 | 105 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 106 | lcd.printf(" Kp Ki Kd"); |
gunarthon | 40:19d51f6e6800 | 107 | lcd.locate(6*parameter, 0); |
gunarthon | 40:19d51f6e6800 | 108 | lcd.printf("*"); |
gunarthon | 40:19d51f6e6800 | 109 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 110 | lcd.printf("%d", int(1000*pidController->getKp())); |
gunarthon | 40:19d51f6e6800 | 111 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 112 | lcd.printf("%d", int(1000*pidController->getKi())); |
gunarthon | 40:19d51f6e6800 | 113 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 114 | lcd.printf("%d", int(1000*pidController->getKd())); |
gunarthon | 40:19d51f6e6800 | 115 | } |
gunarthon | 40:19d51f6e6800 | 116 | else if(menu == menuSIMULATE) |
gunarthon | 40:19d51f6e6800 | 117 | { |
gunarthon | 40:19d51f6e6800 | 118 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 119 | if(simul == simulNONE) |
gunarthon | 41:3bc2a3885b9d | 120 | { |
gunarthon | 41:3bc2a3885b9d | 121 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 122 | lcd.printf("u:STEP * d:RAMP"); |
gunarthon | 41:3bc2a3885b9d | 123 | else |
gunarthon | 41:3bc2a3885b9d | 124 | lcd.printf("u:STEP _ d:RAMP"); |
gunarthon | 41:3bc2a3885b9d | 125 | } |
gunarthon | 40:19d51f6e6800 | 126 | else |
gunarthon | 40:19d51f6e6800 | 127 | lcd.printf("set $in$ out"); |
gunarthon | 40:19d51f6e6800 | 128 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 129 | lcd.printf("%d", int(1000*pidController->getSetPoint())); |
gunarthon | 40:19d51f6e6800 | 130 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 131 | lcd.printf("%d", int(1000*lastInput)); |
gunarthon | 40:19d51f6e6800 | 132 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 133 | lcd.printf("%d", int(1000*lastPwm)); |
gunarthon | 40:19d51f6e6800 | 134 | } |
gunarthon | 40:19d51f6e6800 | 135 | Thread::wait(displayRefreshTime); |
Jonathan Austin |
0:2757d7abb7d9 | 136 | } |
Jonathan Austin |
0:2757d7abb7d9 | 137 | } |
Jonathan Austin |
1:846c97078558 | 138 | |
gunarthon | 38:b760c09b311c | 139 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 140 | |
gunarthon | 38:b760c09b311c | 141 | void PidMethode(void) |
gunarthon | 38:b760c09b311c | 142 | { |
gunarthon | 38:b760c09b311c | 143 | while(true) |
gunarthon | 38:b760c09b311c | 144 | { |
gunarthon | 38:b760c09b311c | 145 | osEvent ev = messageQueue.get(osWaitForever); |
gunarthon | 38:b760c09b311c | 146 | if (ev.status == osEventMessage) |
gunarthon | 38:b760c09b311c | 147 | { |
gunarthon | 38:b760c09b311c | 148 | message_t *message = (message_t*)ev.value.p; |
gunarthon | 38:b760c09b311c | 149 | |
gunarthon | 40:19d51f6e6800 | 150 | double input = message->input; |
gunarthon | 38:b760c09b311c | 151 | |
gunarthon | 38:b760c09b311c | 152 | double newPwm = pidController->getPwm(input); |
gunarthon | 38:b760c09b311c | 153 | |
gunarthon | 38:b760c09b311c | 154 | outPin.write(newPwm); |
gunarthon | 38:b760c09b311c | 155 | |
gunarthon | 38:b760c09b311c | 156 | mpool.free(message); |
gunarthon | 38:b760c09b311c | 157 | } |
gunarthon | 38:b760c09b311c | 158 | } |
gunarthon | 38:b760c09b311c | 159 | } |
gunarthon | 38:b760c09b311c | 160 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 161 | |
gunarthon | 42:b69538bba4f9 | 162 | void SerialMethode(void) |
gunarthon | 38:b760c09b311c | 163 | { |
gunarthon | 39:e4f5710b2f31 | 164 | Serial serial(USBTX, USBRX); |
gunarthon | 39:e4f5710b2f31 | 165 | serial.baud(BAUDRATE); |
gunarthon | 39:e4f5710b2f31 | 166 | serial.format(8, SerialBase::None, 1); |
gunarthon | 40:19d51f6e6800 | 167 | double input = 0; |
gunarthon | 39:e4f5710b2f31 | 168 | |
gunarthon | 38:b760c09b311c | 169 | while(true) |
gunarthon | 38:b760c09b311c | 170 | { |
gunarthon | 40:19d51f6e6800 | 171 | input = double(serial.getc()+(serial.getc()<<8)) / heightResolution; |
gunarthon | 38:b760c09b311c | 172 | message_t *message = mpool.alloc(); |
gunarthon | 38:b760c09b311c | 173 | message->input = input; |
gunarthon | 38:b760c09b311c | 174 | messageQueue.put(message); |
gunarthon | 38:b760c09b311c | 175 | } |
gunarthon | 38:b760c09b311c | 176 | } |
gunarthon | 42:b69538bba4f9 | 177 | //------------------------------------------------------------------------------ |
gunarthon | 42:b69538bba4f9 | 178 | |
gunarthon | 42:b69538bba4f9 | 179 | void UdpMethode(void) |
gunarthon | 42:b69538bba4f9 | 180 | { |
gunarthon | 42:b69538bba4f9 | 181 | UDPSocket socket(&net); |
gunarthon | 42:b69538bba4f9 | 182 | socket.bind(host_port); |
gunarthon | 42:b69538bba4f9 | 183 | SocketAddress socketAddress; |
gunarthon | 42:b69538bba4f9 | 184 | socket.set_blocking(true); |
gunarthon | 42:b69538bba4f9 | 185 | uint16_t packet = 0; |
gunarthon | 42:b69538bba4f9 | 186 | double input = 0; |
gunarthon | 42:b69538bba4f9 | 187 | while(true) |
gunarthon | 42:b69538bba4f9 | 188 | { |
gunarthon | 42:b69538bba4f9 | 189 | //socket.sendto(host_address, host_port, (const void*)input, sizeof(input)); |
gunarthon | 42:b69538bba4f9 | 190 | //input += 1; |
gunarthon | 42:b69538bba4f9 | 191 | socket.recvfrom(&socketAddress, &packet, sizeof(packet)); |
gunarthon | 42:b69538bba4f9 | 192 | |
gunarthon | 42:b69538bba4f9 | 193 | input = double(packet) / heightResolution; |
gunarthon | 42:b69538bba4f9 | 194 | message_t *message = mpool.alloc(); |
gunarthon | 42:b69538bba4f9 | 195 | message->input = input; |
gunarthon | 42:b69538bba4f9 | 196 | messageQueue.put(message); |
gunarthon | 42:b69538bba4f9 | 197 | Thread::wait(300); |
gunarthon | 42:b69538bba4f9 | 198 | } |
gunarthon | 42:b69538bba4f9 | 199 | } |
gunarthon | 42:b69538bba4f9 | 200 | |
gunarthon | 42:b69538bba4f9 | 201 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 202 | |
gunarthon | 40:19d51f6e6800 | 203 | unsigned readButtons() |
gunarthon | 38:b760c09b311c | 204 | { |
gunarthon | 38:b760c09b311c | 205 | double buttonsValue = buttons.read(); |
gunarthon | 38:b760c09b311c | 206 | |
gunarthon | 38:b760c09b311c | 207 | unsigned button = btnNONE; |
gunarthon | 38:b760c09b311c | 208 | if(buttonsValue < 0.08) //0.000 |
gunarthon | 38:b760c09b311c | 209 | button = btnRIGHT; |
gunarthon | 38:b760c09b311c | 210 | else if(buttonsValue < 0.28) //0.170 |
gunarthon | 38:b760c09b311c | 211 | button = btnUP; |
gunarthon | 38:b760c09b311c | 212 | else if(buttonsValue < 0.51) //0.397 |
gunarthon | 38:b760c09b311c | 213 | button = btnDOWN; |
gunarthon | 38:b760c09b311c | 214 | else if(buttonsValue < 0.78) //0.621 |
gunarthon | 38:b760c09b311c | 215 | button = btnLEFT; |
gunarthon | 38:b760c09b311c | 216 | else if(buttonsValue < 0.97) //0.936 |
gunarthon | 38:b760c09b311c | 217 | button = btnSELECT; |
gunarthon | 38:b760c09b311c | 218 | else //1.000 |
gunarthon | 38:b760c09b311c | 219 | button = btnNONE; |
gunarthon | 38:b760c09b311c | 220 | |
gunarthon | 40:19d51f6e6800 | 221 | return button; |
gunarthon | 40:19d51f6e6800 | 222 | |
gunarthon | 40:19d51f6e6800 | 223 | } |
gunarthon | 40:19d51f6e6800 | 224 | //------------------------------------------------------------------------------ |
gunarthon | 40:19d51f6e6800 | 225 | //human-machine interface |
gunarthon | 40:19d51f6e6800 | 226 | void hmiMethode(void) |
gunarthon | 40:19d51f6e6800 | 227 | { |
gunarthon | 40:19d51f6e6800 | 228 | |
gunarthon | 40:19d51f6e6800 | 229 | unsigned button = btnNONE; |
gunarthon | 41:3bc2a3885b9d | 230 | bool fastChange = false; |
gunarthon | 40:19d51f6e6800 | 231 | while(true) |
gunarthon | 40:19d51f6e6800 | 232 | { |
gunarthon | 38:b760c09b311c | 233 | |
gunarthon | 41:3bc2a3885b9d | 234 | button = readButtons(); |
gunarthon | 41:3bc2a3885b9d | 235 | while(button == btnNONE) |
gunarthon | 41:3bc2a3885b9d | 236 | { |
gunarthon | 40:19d51f6e6800 | 237 | button = readButtons(); |
gunarthon | 41:3bc2a3885b9d | 238 | Thread::wait(10); |
gunarthon | 41:3bc2a3885b9d | 239 | fastChange = false; |
gunarthon | 41:3bc2a3885b9d | 240 | } |
gunarthon | 40:19d51f6e6800 | 241 | |
gunarthon | 38:b760c09b311c | 242 | double prevSetPoint = pidController->getSetPoint(); |
gunarthon | 38:b760c09b311c | 243 | double newSetPoint = prevSetPoint; |
gunarthon | 40:19d51f6e6800 | 244 | |
gunarthon | 40:19d51f6e6800 | 245 | if(button == btnSELECT) |
gunarthon | 40:19d51f6e6800 | 246 | { |
gunarthon | 40:19d51f6e6800 | 247 | menu = (menu+1) % menuSIZE; |
gunarthon | 40:19d51f6e6800 | 248 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 249 | } |
gunarthon | 40:19d51f6e6800 | 250 | else if(menu == menuSETPOINT) |
gunarthon | 40:19d51f6e6800 | 251 | { |
gunarthon | 40:19d51f6e6800 | 252 | if(button == btnUP) |
gunarthon | 41:3bc2a3885b9d | 253 | newSetPoint += fastChange ? configFastStep : configStep; |
gunarthon | 40:19d51f6e6800 | 254 | else if(button == btnDOWN) |
gunarthon | 41:3bc2a3885b9d | 255 | newSetPoint -= fastChange ? configFastStep : configStep; |
gunarthon | 40:19d51f6e6800 | 256 | else if(button == btnLEFT) |
gunarthon | 41:3bc2a3885b9d | 257 | newSetPoint -= fastChange ? 10*configFastStep : 10*configStep; |
gunarthon | 40:19d51f6e6800 | 258 | else if(button == btnRIGHT) |
gunarthon | 41:3bc2a3885b9d | 259 | newSetPoint += fastChange ? 10*configFastStep : 10*configStep; |
gunarthon | 40:19d51f6e6800 | 260 | |
gunarthon | 40:19d51f6e6800 | 261 | pidController->setSetPoint(newSetPoint); |
gunarthon | 40:19d51f6e6800 | 262 | } |
gunarthon | 40:19d51f6e6800 | 263 | else if(menu == menuPID) |
gunarthon | 40:19d51f6e6800 | 264 | { |
gunarthon | 40:19d51f6e6800 | 265 | if(button == btnUP) |
gunarthon | 40:19d51f6e6800 | 266 | { |
gunarthon | 40:19d51f6e6800 | 267 | if(parameter == parameterKP) |
gunarthon | 41:3bc2a3885b9d | 268 | pidController->addKp(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 269 | else if(parameter == parameterKI) |
gunarthon | 41:3bc2a3885b9d | 270 | pidController->addKi(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 271 | else if(parameter == parameterKD) |
gunarthon | 41:3bc2a3885b9d | 272 | pidController->addKd(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 273 | } |
gunarthon | 40:19d51f6e6800 | 274 | else if(button == btnDOWN) |
gunarthon | 40:19d51f6e6800 | 275 | { |
gunarthon | 40:19d51f6e6800 | 276 | if(parameter == parameterKP) |
gunarthon | 41:3bc2a3885b9d | 277 | pidController->addKp(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 278 | else if(parameter == parameterKI) |
gunarthon | 41:3bc2a3885b9d | 279 | pidController->addKi(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 280 | else if(parameter == parameterKD) |
gunarthon | 41:3bc2a3885b9d | 281 | pidController->addKd(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 282 | } |
gunarthon | 40:19d51f6e6800 | 283 | else if(button == btnLEFT) |
gunarthon | 40:19d51f6e6800 | 284 | parameter = (parameter+1) % parameterSIZE; |
gunarthon | 40:19d51f6e6800 | 285 | else if(button == btnRIGHT) |
gunarthon | 40:19d51f6e6800 | 286 | pidController->setParameters(Kp, Ki, Kd); |
gunarthon | 40:19d51f6e6800 | 287 | } |
gunarthon | 40:19d51f6e6800 | 288 | |
gunarthon | 40:19d51f6e6800 | 289 | else if(menu == menuSIMULATE) |
gunarthon | 40:19d51f6e6800 | 290 | { |
gunarthon | 40:19d51f6e6800 | 291 | if(button == btnUP) //step |
gunarthon | 40:19d51f6e6800 | 292 | { |
gunarthon | 40:19d51f6e6800 | 293 | simul = simulSTEP; |
gunarthon | 40:19d51f6e6800 | 294 | for(unsigned i = 0; i < cameraFPS * simulationSeconds; i++) |
gunarthon | 40:19d51f6e6800 | 295 | { |
gunarthon | 40:19d51f6e6800 | 296 | double input = i < cameraFPS * simulationSeconds / 2 ? 0 : 1; |
gunarthon | 40:19d51f6e6800 | 297 | message_t *message = mpool.alloc(); |
gunarthon | 40:19d51f6e6800 | 298 | message->input = input; |
gunarthon | 40:19d51f6e6800 | 299 | messageQueue.put(message); |
gunarthon | 40:19d51f6e6800 | 300 | |
gunarthon | 40:19d51f6e6800 | 301 | Thread::wait(1000/cameraFPS); |
gunarthon | 40:19d51f6e6800 | 302 | } |
gunarthon | 40:19d51f6e6800 | 303 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 304 | } |
gunarthon | 40:19d51f6e6800 | 305 | else if(button == btnDOWN) //ramp |
gunarthon | 40:19d51f6e6800 | 306 | { |
gunarthon | 40:19d51f6e6800 | 307 | simul = simulRAMP; |
gunarthon | 40:19d51f6e6800 | 308 | for(unsigned i = 0; i < cameraFPS * simulationSeconds; i++) |
gunarthon | 40:19d51f6e6800 | 309 | { |
gunarthon | 40:19d51f6e6800 | 310 | double input = double(i) / ((cameraFPS * simulationSeconds)-1); |
gunarthon | 40:19d51f6e6800 | 311 | message_t *message = mpool.alloc(); |
gunarthon | 40:19d51f6e6800 | 312 | message->input = input; |
gunarthon | 40:19d51f6e6800 | 313 | messageQueue.put(message); |
gunarthon | 40:19d51f6e6800 | 314 | |
gunarthon | 40:19d51f6e6800 | 315 | Thread::wait(1000/cameraFPS); |
gunarthon | 40:19d51f6e6800 | 316 | } |
gunarthon | 40:19d51f6e6800 | 317 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 318 | } |
gunarthon | 41:3bc2a3885b9d | 319 | else if(button == btnLEFT) |
gunarthon | 41:3bc2a3885b9d | 320 | setPointSimul = true; |
gunarthon | 40:19d51f6e6800 | 321 | else if(button == btnRIGHT) |
gunarthon | 41:3bc2a3885b9d | 322 | setPointSimul = false; |
gunarthon | 38:b760c09b311c | 323 | |
gunarthon | 40:19d51f6e6800 | 324 | } |
gunarthon | 41:3bc2a3885b9d | 325 | unsigned repeatCount = 0; |
gunarthon | 41:3bc2a3885b9d | 326 | while(button != btnNONE && !fastChange) |
gunarthon | 40:19d51f6e6800 | 327 | { |
gunarthon | 41:3bc2a3885b9d | 328 | repeatCount++; |
gunarthon | 40:19d51f6e6800 | 329 | button = readButtons(); |
gunarthon | 40:19d51f6e6800 | 330 | Thread::wait(10); |
gunarthon | 41:3bc2a3885b9d | 331 | if(repeatCount > 50) |
gunarthon | 41:3bc2a3885b9d | 332 | fastChange = true; |
gunarthon | 41:3bc2a3885b9d | 333 | } |
gunarthon | 38:b760c09b311c | 334 | Thread::wait(100); |
gunarthon | 38:b760c09b311c | 335 | } |
gunarthon | 38:b760c09b311c | 336 | } |
gunarthon | 38:b760c09b311c | 337 | |
gunarthon | 38:b760c09b311c | 338 | //=============================Main Thread====================================== |
gunarthon | 38:b760c09b311c | 339 | // main() runs in its own thread in the OS |
gunarthon | 38:b760c09b311c | 340 | |
gunarthon | 38:b760c09b311c | 341 | int main() { |
gunarthon | 38:b760c09b311c | 342 | |
gunarthon | 38:b760c09b311c | 343 | //led1.period(4.0f); // 4 second period |
gunarthon | 38:b760c09b311c | 344 | |
gunarthon | 38:b760c09b311c | 345 | lcd.cls(); |
gunarthon | 38:b760c09b311c | 346 | lcd.printf("HELLO"); |
gunarthon | 38:b760c09b311c | 347 | |
gunarthon | 42:b69538bba4f9 | 348 | Thread::wait(2000); |
gunarthon | 42:b69538bba4f9 | 349 | if(readButtons() == btnDOWN) |
gunarthon | 42:b69538bba4f9 | 350 | useUDP = true; |
gunarthon | 42:b69538bba4f9 | 351 | |
gunarthon | 42:b69538bba4f9 | 352 | if(useUDP) |
gunarthon | 42:b69538bba4f9 | 353 | { |
gunarthon | 42:b69538bba4f9 | 354 | lcd.cls(); |
gunarthon | 42:b69538bba4f9 | 355 | lcd.printf("Connecting..."); |
gunarthon | 42:b69538bba4f9 | 356 | net.connect(); |
gunarthon | 42:b69538bba4f9 | 357 | |
gunarthon | 42:b69538bba4f9 | 358 | // Show the network address |
gunarthon | 42:b69538bba4f9 | 359 | const char *ip = net.get_ip_address(); |
gunarthon | 42:b69538bba4f9 | 360 | lcd.cls(); |
gunarthon | 42:b69538bba4f9 | 361 | lcd.printf("%s", ip ? ip : "No IP"); |
gunarthon | 42:b69538bba4f9 | 362 | if(!ip) |
gunarthon | 42:b69538bba4f9 | 363 | while(1); //terminate program |
gunarthon | 42:b69538bba4f9 | 364 | |
gunarthon | 42:b69538bba4f9 | 365 | Thread::wait(5000); |
gunarthon | 42:b69538bba4f9 | 366 | udpThread.start(UdpMethode); |
gunarthon | 42:b69538bba4f9 | 367 | } |
gunarthon | 42:b69538bba4f9 | 368 | else |
gunarthon | 42:b69538bba4f9 | 369 | serialThread.start(SerialMethode); |
gunarthon | 42:b69538bba4f9 | 370 | |
gunarthon | 38:b760c09b311c | 371 | pidController = new Pid(Kp, Ki, Kd); |
gunarthon | 38:b760c09b311c | 372 | pidController->setSetPoint(setPoint); |
gunarthon | 38:b760c09b311c | 373 | |
gunarthon | 41:3bc2a3885b9d | 374 | lcdThread.start(LcdMethode); |
gunarthon | 38:b760c09b311c | 375 | pidThread.start(PidMethode); |
gunarthon | 38:b760c09b311c | 376 | hmiThread.start(hmiMethode); |
gunarthon | 41:3bc2a3885b9d | 377 | setPointThread.start(setPointMethode); |
gunarthon | 38:b760c09b311c | 378 | |
gunarthon | 38:b760c09b311c | 379 | while (true) { |
gunarthon | 38:b760c09b311c | 380 | Thread::wait(1100); |
gunarthon | 38:b760c09b311c | 381 | } |
gunarthon | 38:b760c09b311c | 382 | } |
gunarthon | 38:b760c09b311c | 383 | |
gunarthon | 38:b760c09b311c | 384 | //============================================================================== |
gunarthon | 38:b760c09b311c | 385 | |
gunarthon | 38:b760c09b311c | 386 | |
gunarthon | 38:b760c09b311c | 387 |