Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Tue Jun 19 11:22:46 2012 +0000
Revision:
28:44b7b6e35548
Parent:
27:1ce994629ffc
Child:
29:2fc8c12822eb
version almost ready for AsahiTV;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:345b3bc7a0ea 1 #include "mbed.h"
mbedalvaro 0:345b3bc7a0ea 2 #include "hardwareIO.h"
mbedalvaro 0:345b3bc7a0ea 3 #include "mbedOSC.h"
mbedalvaro 0:345b3bc7a0ea 4 #include "blobConfig.h"
mbedalvaro 0:345b3bc7a0ea 5 #include "simpleLaserRenderer.h"
mbedalvaro 0:345b3bc7a0ea 6
mbedalvaro 0:345b3bc7a0ea 7 extern "C" void mbed_reset();
mbedalvaro 0:345b3bc7a0ea 8
mbedalvaro 0:345b3bc7a0ea 9 blobConfig blobconf;
mbedalvaro 0:345b3bc7a0ea 10 simpleLaserSensingRenderer lsr;
mbedalvaro 0:345b3bc7a0ea 11
mbedalvaro 0:345b3bc7a0ea 12 // For tests:
mbedalvaro 0:345b3bc7a0ea 13 DigitalOut myled(LED1);
mbedalvaro 0:345b3bc7a0ea 14 DigitalOut myled2(LED2);
mbedalvaro 23:bf666fcc61bc 15 DigitalOut myled3(LED3);
mbedalvaro 0:345b3bc7a0ea 16
mbedalvaro 0:345b3bc7a0ea 17
mbedalvaro 1:a4050fee11f7 18 // To test the time it takes for executing one loop in the main program:
mbedalvaro 10:6f8e48dca1bd 19 //#define LOOPTIMECOMPUTE
mbedalvaro 1:a4050fee11f7 20
mbedalvaro 1:a4050fee11f7 21 // To get serial commands (for debug, or other things using a Terminal - for instance, a laser scan)
mbedalvaro 14:0fc33a3a7b4b 22 #define SERIAL_COMMANDS
mbedalvaro 0:345b3bc7a0ea 23
mbedalvaro 0:345b3bc7a0ea 24 //---------- ETHERNET / OSC related (in the future, put somewhere else...): -------------------------------------------
mbedalvaro 0:345b3bc7a0ea 25 // mbed IP address (server):
mbedalvaro 0:345b3bc7a0ea 26 #ifdef DHCP
mbedalvaro 0:345b3bc7a0ea 27 EthernetNetIf eth;
mbedalvaro 0:345b3bc7a0ea 28 #else
mbedalvaro 0:345b3bc7a0ea 29 EthernetNetIf eth(
mbedalvaro 1:a4050fee11f7 30 IpAddr(10,0,0,2), //IP Address
mbedalvaro 1:a4050fee11f7 31 IpAddr(255,255,255,0), //Network Mask
mbedalvaro 1:a4050fee11f7 32 IpAddr(10,0,0,1), //Gateway
mbedalvaro 1:a4050fee11f7 33 IpAddr(10,0,0,1) //DNS
mbedalvaro 0:345b3bc7a0ea 34 );
mbedalvaro 0:345b3bc7a0ea 35 #endif
mbedalvaro 0:345b3bc7a0ea 36
mbedalvaro 0:345b3bc7a0ea 37 //uint8_t serverMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
mbedalvaro 0:345b3bc7a0ea 38 uint8_t serverIp[] = { 10, 0, 0, 2 }; // not needed perhaps!
mbedalvaro 0:345b3bc7a0ea 39 int serverPort = 10000;
mbedalvaro 1:a4050fee11f7 40
mbedalvaro 21:bc9b9383f4b6 41 uint8_t destIp[] = {10, 0, 0, 3};
mbedalvaro 0:345b3bc7a0ea 42 int destPort = 12000;
mbedalvaro 1:a4050fee11f7 43
mbedalvaro 0:345b3bc7a0ea 44 char *topAddress="/mbed";
mbedalvaro 0:345b3bc7a0ea 45 char *subAddress[3]={ "/test1" , "/test2" , "/test3" };
mbedalvaro 1:a4050fee11f7 46
mbedalvaro 0:345b3bc7a0ea 47 OSCMessage recMes;
mbedalvaro 0:345b3bc7a0ea 48 OSCMessage sendMes;
mbedalvaro 1:a4050fee11f7 49
mbedalvaro 1:a4050fee11f7 50 OSCClass osc;
mbedalvaro 1:a4050fee11f7 51 //OSCClass osc(&recMes); // instantiate OSC communication object, and set the receiver container from the OSC packets
mbedalvaro 1:a4050fee11f7 52
mbedalvaro 1:a4050fee11f7 53 void processOSC(UDPSocketEvent e);
mbedalvaro 0:345b3bc7a0ea 54 // ----------------------------------------------------------------------------------------------------------------------
mbedalvaro 0:345b3bc7a0ea 55
mbedalvaro 2:34157ebbf56b 56 // Tickers:
mbedalvaro 0:345b3bc7a0ea 57 Ticker timerForRendering;
mbedalvaro 2:34157ebbf56b 58 //Ticker timerForSendingData; // better use a timer, so as not to interrupt the exact laser display ticker
mbedalvaro 2:34157ebbf56b 59
mbedalvaro 9:3321170d157c 60 // Timers:
mbedalvaro 0:345b3bc7a0ea 61 Timer measureLoopPeriod;
mbedalvaro 9:3321170d157c 62 //Timer measureUpdatePeriod;
mbedalvaro 18:d72935b13858 63
mbedalvaro 0:345b3bc7a0ea 64
mbedalvaro 1:a4050fee11f7 65 // to get serial commands (not necessary perhaps)
mbedalvaro 0:345b3bc7a0ea 66 void processSerial();
mbedalvaro 1:a4050fee11f7 67
mbedalvaro 0:345b3bc7a0ea 68 int main() {
mbedalvaro 1:a4050fee11f7 69
mbedalvaro 0:345b3bc7a0ea 70 // Initialize the hardware (laser powers, positions...):
mbedalvaro 0:345b3bc7a0ea 71 IO.init();
mbedalvaro 0:345b3bc7a0ea 72
mbedalvaro 1:a4050fee11f7 73 // -------------------------------
mbedalvaro 1:a4050fee11f7 74 // Set the Ethernet port:
mbedalvaro 1:a4050fee11f7 75 printf("Setting up...\r\n");
mbedalvaro 1:a4050fee11f7 76 EthernetErr ethErr = eth.setup();
mbedalvaro 1:a4050fee11f7 77 if (ethErr) {
mbedalvaro 1:a4050fee11f7 78 printf("Error %d in setup.\r\n", ethErr);
mbedalvaro 1:a4050fee11f7 79 return -1;
mbedalvaro 1:a4050fee11f7 80 }
mbedalvaro 1:a4050fee11f7 81 printf("Setup OK\r\n");
mbedalvaro 1:a4050fee11f7 82
mbedalvaro 1:a4050fee11f7 83 //(1) Sending message:
mbedalvaro 1:a4050fee11f7 84 // Set IP and Port:
mbedalvaro 1:a4050fee11f7 85 sendMes.setIp( destIp );
mbedalvaro 1:a4050fee11f7 86 sendMes.setPort( destPort );
mbedalvaro 1:a4050fee11f7 87 // Set data:
mbedalvaro 1:a4050fee11f7 88 // sendMes.setTopAddress(topAddress);
mbedalvaro 1:a4050fee11f7 89
mbedalvaro 1:a4050fee11f7 90 //setting osc functionnality:
mbedalvaro 1:a4050fee11f7 91 //(2) Receiving:
mbedalvaro 1:a4050fee11f7 92 // recMes.setIp( serverIp ); // not needed?
mbedalvaro 1:a4050fee11f7 93 osc.setReceiveMessage(&recMes); // this sets the receiver container for the OSC packets (we can avoid doing this if we use osc.getMessage() to get messages)
mbedalvaro 1:a4050fee11f7 94 osc.begin(serverPort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
mbedalvaro 1:a4050fee11f7 95 // -------------------------------
mbedalvaro 1:a4050fee11f7 96
mbedalvaro 0:345b3bc7a0ea 97 /* // sending seems not to work right after setting the osc object??
mbedalvaro 0:345b3bc7a0ea 98 wait(1);
mbedalvaro 0:345b3bc7a0ea 99 sendMes.setTopAddress("starting");
mbedalvaro 1:a4050fee11f7 100 sendMes.setSubAddress("");
mbedalvaro 0:345b3bc7a0ea 101 osc.sendOsc( &sendMes );
mbedalvaro 0:345b3bc7a0ea 102 */
mbedalvaro 1:a4050fee11f7 103
mbedalvaro 0:345b3bc7a0ea 104 // initialize with the desired blob configuration:
mbedalvaro 1:a4050fee11f7 105
mbedalvaro 0:345b3bc7a0ea 106 // Tested modes:
mbedalvaro 1:a4050fee11f7 107 blobconf.clearConfig();
mbedalvaro 10:6f8e48dca1bd 108 // blobconf.addOneElasticLoopContractCentral();
mbedalvaro 11:62f7183a03e7 109 // blobconf.addOneElasticContourFollowing();
mbedalvaro 1:a4050fee11f7 110
mbedalvaro 14:0fc33a3a7b4b 111 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 9:3321170d157c 112 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 16:2ff1cb2ae1b1 113 //blobconf.addOneRigidLoopFollowing();
mbedalvaro 16:2ff1cb2ae1b1 114 blobconf.addOneRigidLoopFollowing();
mbedalvaro 2:34157ebbf56b 115
mbedalvaro 7:0df17f3078bc 116
mbedalvaro 5:73cd58b58f95 117 //blobconf.addOneRigidLoopTest();
mbedalvaro 1:a4050fee11f7 118
mbedalvaro 0:345b3bc7a0ea 119 // Important: first, set the initial position for all the blobs, this will be useful because
mbedalvaro 0:345b3bc7a0ea 120 // when changing modes we can use the previous central position...
mbedalvaro 1:a4050fee11f7 121 // blobconf.setInitialPos(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_Y);
mbedalvaro 1:a4050fee11f7 122
mbedalvaro 24:4e52031a495b 123 // draw the config once before activating the laser buffer:
mbedalvaro 5:73cd58b58f95 124 blobconf.draw();
mbedalvaro 13:9f03eac02700 125 lsr.startFullDisplay();
mbedalvaro 1:a4050fee11f7 126
mbedalvaro 0:345b3bc7a0ea 127 // RENRERER (attn: setConfigToRender must be called when the blobconf is set - i.e., the number of blobs and number of points/blob is fixed)
mbedalvaro 1:a4050fee11f7 128 lsr.setConfigToRender(&blobconf);
mbedalvaro 1:a4050fee11f7 129
mbedalvaro 1:a4050fee11f7 130 // Timer on the rendering function of the oneLoop object:
mbedalvaro 9:3321170d157c 131 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 9:3321170d157c 132 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 1:a4050fee11f7 133
mbedalvaro 1:a4050fee11f7 134 // Timer for sending OSC data:
mbedalvaro 9:3321170d157c 135 // timerForSendingData.attach(&blobconf, &blobConfig::sendConfData, 0.025); // time in seconds (25ms -> 40Hz)
mbedalvaro 1:a4050fee11f7 136
mbedalvaro 1:a4050fee11f7 137 //========================================== INFINITE LOOP (in USER PROGRAM CONTEXT) ===================================================================
mbedalvaro 1:a4050fee11f7 138 #ifdef LOOPTIMECOMPUTE
mbedalvaro 1:a4050fee11f7 139 int timeCounterNum=1000;
mbedalvaro 1:a4050fee11f7 140 #endif
mbedalvaro 1:a4050fee11f7 141
mbedalvaro 9:3321170d157c 142 //measureUpdatePeriod.start();
mbedalvaro 1:a4050fee11f7 143
mbedalvaro 1:a4050fee11f7 144 while (true) {
mbedalvaro 1:a4050fee11f7 145
mbedalvaro 13:9f03eac02700 146 if (lsr.endedFullDisplay()) {
mbedalvaro 9:3321170d157c 147
mbedalvaro 9:3321170d157c 148 // measureUpdatePeriod.stop();
mbedalvaro 9:3321170d157c 149 // measureUpdatePeriod.reset();
mbedalvaro 9:3321170d157c 150
mbedalvaro 9:3321170d157c 151 // __disable_irq();
mbedalvaro 9:3321170d157c 152
mbedalvaro 1:a4050fee11f7 153 // update config dynamics (this also could be threaded?):
mbedalvaro 1:a4050fee11f7 154 blobconf.update();
mbedalvaro 1:a4050fee11f7 155
mbedalvaro 10:6f8e48dca1bd 156
mbedalvaro 1:a4050fee11f7 157 // draw the config (note: each kind of blob renders differently)
mbedalvaro 1:a4050fee11f7 158 blobconf.draw();
mbedalvaro 18:d72935b13858 159
mbedalvaro 18:d72935b13858 160
mbedalvaro 18:d72935b13858 161 // (b)Sending Data: // PUT THIS IN AN INTERRUPT OR USE A TIMER!!! it may be TOO FAST...
mbedalvaro 18:d72935b13858 162 // NOTE: better use a timer, so the only ISR "ticker" is the laser rendering (otherwise the laser rendering will be interrupted by the sending of data - the other way is ok):
mbedalvaro 18:d72935b13858 163 // NOTE: timer for sending is now not COMMON to all blobs, but depends on the blob (it could depend on the CONFIG only, but this can be simulated by using
mbedalvaro 18:d72935b13858 164 // per blob timer counter.
mbedalvaro 18:d72935b13858 165 blobconf.sendConfData();
mbedalvaro 18:d72935b13858 166
mbedalvaro 16:2ff1cb2ae1b1 167
mbedalvaro 23:bf666fcc61bc 168 lsr.startFullDisplay(); // this start the point-display counter (wherever the actual laser is). Before update and draw, the counter needs to be reset.
mbedalvaro 16:2ff1cb2ae1b1 169
mbedalvaro 9:3321170d157c 170
mbedalvaro 9:3321170d157c 171 // __enable_irq();
mbedalvaro 9:3321170d157c 172
mbedalvaro 9:3321170d157c 173 // measureUpdatePeriod.start();
mbedalvaro 10:6f8e48dca1bd 174
mbedalvaro 1:a4050fee11f7 175 }
mbedalvaro 0:345b3bc7a0ea 176
mbedalvaro 2:34157ebbf56b 177
mbedalvaro 1:a4050fee11f7 178 // COMMUNICATION:
mbedalvaro 1:a4050fee11f7 179 // (a) Reading commands:
mbedalvaro 1:a4050fee11f7 180 // Ethernet:
mbedalvaro 1:a4050fee11f7 181 Net::poll(); // this will take care of calling processOSC(UDPSocketEvent e) when a new packet arrives.
mbedalvaro 1:a4050fee11f7 182
mbedalvaro 1:a4050fee11f7 183 // Serial:
mbedalvaro 1:a4050fee11f7 184 #ifdef SERIAL_COMMANDS
mbedalvaro 1:a4050fee11f7 185 if (pc.readable()>0) processSerial();
mbedalvaro 1:a4050fee11f7 186 #endif
mbedalvaro 9:3321170d157c 187
mbedalvaro 9:3321170d157c 188
mbedalvaro 1:a4050fee11f7 189 // text:
mbedalvaro 1:a4050fee11f7 190 /*
mbedalvaro 1:a4050fee11f7 191 sendMes.setTopAddress("/hello");
mbedalvaro 1:a4050fee11f7 192 sendMes.setSubAddress("/daito"); // ATTENTION: the host computer needs to know in advance how many points are in the loop (I did not implement "bundle" messages yet...)
mbedalvaro 1:a4050fee11f7 193 int x=(long)10;
mbedalvaro 1:a4050fee11f7 194 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 195 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 196 */
mbedalvaro 1:a4050fee11f7 197
mbedalvaro 0:345b3bc7a0ea 198 #ifdef LOOPTIMECOMPUTE
mbedalvaro 9:3321170d157c 199 if (timeCounterNum>50) myled = 0;
mbedalvaro 9:3321170d157c 200 // if (timeCounterNum%10==0) blobconf.sendConfData();
mbedalvaro 9:3321170d157c 201 if (timeCounterNum>100) {
mbedalvaro 1:a4050fee11f7 202 myled = 1;
mbedalvaro 1:a4050fee11f7 203 measureLoopPeriod.stop();
mbedalvaro 1:a4050fee11f7 204 sendMes.setTopAddress("/timeloop");
mbedalvaro 1:a4050fee11f7 205 sendMes.setSubAddress("/");
mbedalvaro 10:6f8e48dca1bd 206 // long x=(long)(int(measureLoopPeriod.read_us()/100.0));
mbedalvaro 9:3321170d157c 207 // long x=(long)(blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory.size());
mbedalvaro 9:3321170d157c 208 // long x=(long)(blobconf.blobArray[0]->normRecenteringVector);
mbedalvaro 10:6f8e48dca1bd 209 long x=(long)(1000*blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory[0].intensity);
mbedalvaro 1:a4050fee11f7 210 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 211 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 212 timeCounterNum=0;
mbedalvaro 1:a4050fee11f7 213 measureLoopPeriod.reset();
mbedalvaro 1:a4050fee11f7 214 measureLoopPeriod.start();
mbedalvaro 1:a4050fee11f7 215 } else timeCounterNum++;
mbedalvaro 0:345b3bc7a0ea 216 #endif
mbedalvaro 0:345b3bc7a0ea 217
mbedalvaro 0:345b3bc7a0ea 218 }
mbedalvaro 0:345b3bc7a0ea 219 }
mbedalvaro 0:345b3bc7a0ea 220
mbedalvaro 1:a4050fee11f7 221 // ================= INTERPRET COMMAND =========================
mbedalvaro 0:345b3bc7a0ea 222 // NOTE: the following arrays are GLOBAL (used in processOSC and processSerial, as well as in interpretCommand function):
mbedalvaro 1:a4050fee11f7 223 // max of two addresses (top and sub), of a max length of 24 characters:
mbedalvaro 0:345b3bc7a0ea 224 char address[2][24];
mbedalvaro 0:345b3bc7a0ea 225 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 226 int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 227
mbedalvaro 1:a4050fee11f7 228 //interpretCommand(const char& address[2][], const int& data[2]) {
mbedalvaro 0:345b3bc7a0ea 229 void interpretCommand() {
mbedalvaro 0:345b3bc7a0ea 230 // (I) =========================================== SPECIAL FUNCTIONS (reset, rescan LUT, etc) ====================================================
mbedalvaro 16:2ff1cb2ae1b1 231 if ( !strcmp(address[0], "takeSnapshot" ) ) {
mbedalvaro 24:4e52031a495b 232 int value=data[0];
mbedalvaro 24:4e52031a495b 233 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 234
mbedalvaro 1:a4050fee11f7 235 // for test:
mbedalvaro 1:a4050fee11f7 236 for (int i=0; i<2 ; i++) {
mbedalvaro 1:a4050fee11f7 237 myled = 1;
mbedalvaro 1:a4050fee11f7 238 wait(0.1);
mbedalvaro 1:a4050fee11f7 239 myled = 0;
mbedalvaro 1:a4050fee11f7 240 wait(0.1);
mbedalvaro 1:a4050fee11f7 241 }
mbedalvaro 1:a4050fee11f7 242
mbedalvaro 1:a4050fee11f7 243 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 244 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 245
mbedalvaro 24:4e52031a495b 246 // Then, do the scan (sending values on SERIAL port):
mbedalvaro 24:4e52031a495b 247 IO.scan_serial(value);
mbedalvaro 1:a4050fee11f7 248
mbedalvaro 1:a4050fee11f7 249 // Finally, start again threaded display:
mbedalvaro 9:3321170d157c 250 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 251 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 24:4e52031a495b 252
mbedalvaro 24:4e52031a495b 253 }
mbedalvaro 1:a4050fee11f7 254 }
mbedalvaro 1:a4050fee11f7 255
mbedalvaro 1:a4050fee11f7 256 else if ( !strcmp(address[0], "mbedReset" ) ) mbed_reset();
mbedalvaro 22:d87317d7ca91 257
mbedalvaro 22:d87317d7ca91 258 else if (!strcmp(address[0], "showMirrorLimits")) {
mbedalvaro 22:d87317d7ca91 259 int value=data[0];
mbedalvaro 22:d87317d7ca91 260 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 22:d87317d7ca91 261 timerForRendering.detach();
mbedalvaro 24:4e52031a495b 262 IO.showLimitsMirrors(value);
mbedalvaro 22:d87317d7ca91 263 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 24:4e52031a495b 264 }
mbedalvaro 22:d87317d7ca91 265 }
mbedalvaro 1:a4050fee11f7 266
mbedalvaro 1:a4050fee11f7 267 else if ( !strcmp(address[0], "calibrate" ) ) {
mbedalvaro 1:a4050fee11f7 268 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 269 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 270 // RESCAN (and save LUT table):
mbedalvaro 1:a4050fee11f7 271 IO.scanLUT();
mbedalvaro 1:a4050fee11f7 272 // Finally, start again threaded display:
mbedalvaro 9:3321170d157c 273 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 274 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 275 }
mbedalvaro 1:a4050fee11f7 276
mbedalvaro 1:a4050fee11f7 277 // (II) ========================================= GLOBAL CONFIG and HARDWARE COMMANDS ===========================================
mbedalvaro 1:a4050fee11f7 278
mbedalvaro 1:a4050fee11f7 279 else if ( !strcmp(address[0], "setGreenLaser" ) ) {
mbedalvaro 1:a4050fee11f7 280 int value=data[0];
mbedalvaro 1:a4050fee11f7 281 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 25:74cb85b85fd2 282 if (value==0)
mbedalvaro 25:74cb85b85fd2 283 blobconf.allSetColor(0x04);// only red (note that we are in blue for "test" mode... so the blue will appear when touching something anyway
mbedalvaro 25:74cb85b85fd2 284 else
mbedalvaro 25:74cb85b85fd2 285 blobconf.allSetColor(0x06); // RED and GREEN (note: in the future, make a method to set colors properly! one by one...)
mbedalvaro 1:a4050fee11f7 286 }
mbedalvaro 1:a4050fee11f7 287 }
mbedalvaro 23:bf666fcc61bc 288
mbedalvaro 23:bf666fcc61bc 289 else if ( !strcmp(address[0], "testPower" ) ) {
mbedalvaro 23:bf666fcc61bc 290 // First, we need to disable the threaded display for the loop:
mbedalvaro 23:bf666fcc61bc 291 timerForRendering.detach();
mbedalvaro 23:bf666fcc61bc 292
mbedalvaro 23:bf666fcc61bc 293 // Note: arguments is first 3 bits to set the laser powers (3 LSB bits to set each color)
mbedalvaro 23:bf666fcc61bc 294 // and then the second argument is the number of seconds to wait for the measurment
mbedalvaro 23:bf666fcc61bc 295 int value1=data[0], value2=data[1];
mbedalvaro 23:bf666fcc61bc 296 if ((value1!=-1)&&(value2!=-1)) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 23:bf666fcc61bc 297
mbedalvaro 23:bf666fcc61bc 298 // Set position of mirrors:
mbedalvaro 23:bf666fcc61bc 299 IO.writeOutX(CENTER_AD_MIRROR_X);
mbedalvaro 23:bf666fcc61bc 300 IO.writeOutY(CENTER_AD_MIRROR_Y);
mbedalvaro 23:bf666fcc61bc 301
mbedalvaro 23:bf666fcc61bc 302 for (int i=0; i<3 ; i++) {
mbedalvaro 23:bf666fcc61bc 303 myled3 = 1;
mbedalvaro 23:bf666fcc61bc 304 wait(0.2);
mbedalvaro 23:bf666fcc61bc 305 myled3 = 0;
mbedalvaro 23:bf666fcc61bc 306 wait(0.2);
mbedalvaro 23:bf666fcc61bc 307 }
mbedalvaro 23:bf666fcc61bc 308
mbedalvaro 23:bf666fcc61bc 309 // Set laser power:
mbedalvaro 23:bf666fcc61bc 310 IO.setRGBPower((unsigned char)value1);
mbedalvaro 23:bf666fcc61bc 311
mbedalvaro 23:bf666fcc61bc 312 // Wait...
mbedalvaro 23:bf666fcc61bc 313 wait(value2);// in seconds
mbedalvaro 23:bf666fcc61bc 314 //Timer t;
mbedalvaro 23:bf666fcc61bc 315 //t.start();
mbedalvaro 23:bf666fcc61bc 316 //while(t.read_ms()<value2*1000);
mbedalvaro 23:bf666fcc61bc 317 //t.stop();
mbedalvaro 23:bf666fcc61bc 318 }
mbedalvaro 23:bf666fcc61bc 319
mbedalvaro 23:bf666fcc61bc 320 // Finally, start again threaded display:
mbedalvaro 23:bf666fcc61bc 321 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 23:bf666fcc61bc 322
mbedalvaro 23:bf666fcc61bc 323 }
mbedalvaro 23:bf666fcc61bc 324
mbedalvaro 23:bf666fcc61bc 325
mbedalvaro 1:a4050fee11f7 326
mbedalvaro 1:a4050fee11f7 327 // SIMPLE BEHAVIOUR MODES (to be read from an XML file in the future):
mbedalvaro 11:62f7183a03e7 328 else if (!strcmp(address[0], "elastic_following")) { //
mbedalvaro 1:a4050fee11f7 329 timerForRendering.detach();
mbedalvaro 0:345b3bc7a0ea 330 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 331 blobconf.clearConfig();
mbedalvaro 1:a4050fee11f7 332 blobconf.addOneElasticContourFollowing();
mbedalvaro 1:a4050fee11f7 333 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 334 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 335 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 336
mbedalvaro 11:62f7183a03e7 337 } else if (!strcmp(address[0], "elastic_mouth")) { //
mbedalvaro 1:a4050fee11f7 338 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 339 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 340 blobconf.clearConfig();
mbedalvaro 1:a4050fee11f7 341 blobconf.addOneElasticLoopContractCentral();
mbedalvaro 1:a4050fee11f7 342 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 343 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 344 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 19:228430f1350e 345
mbedalvaro 19:228430f1350e 346 } else if (!strcmp(address[0], "elastic_mouth_small")) { //
mbedalvaro 19:228430f1350e 347 timerForRendering.detach();
mbedalvaro 19:228430f1350e 348 // blobconf.computeBoundingBox();
mbedalvaro 19:228430f1350e 349 blobconf.clearConfig();
mbedalvaro 19:228430f1350e 350 blobconf.addOneElasticLoopContractCentralFast();
mbedalvaro 19:228430f1350e 351 lsr.setConfigToRender(&blobconf);
mbedalvaro 19:228430f1350e 352 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 19:228430f1350e 353 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 354 }
mbedalvaro 11:62f7183a03e7 355 else if (!strcmp(address[0], "spot_bouncing")) {
mbedalvaro 16:2ff1cb2ae1b1 356 int value=data[0];
mbedalvaro 16:2ff1cb2ae1b1 357 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 16:2ff1cb2ae1b1 358
mbedalvaro 16:2ff1cb2ae1b1 359 timerForRendering.detach();
mbedalvaro 16:2ff1cb2ae1b1 360 // blobconf.computeBoundingBox();
mbedalvaro 16:2ff1cb2ae1b1 361 blobconf.clearConfig();
mbedalvaro 16:2ff1cb2ae1b1 362
mbedalvaro 22:d87317d7ca91 363 // HACK for the time being:
mbedalvaro 24:4e52031a495b 364 /*
mbedalvaro 22:d87317d7ca91 365 int aux=rand()%100;
mbedalvaro 22:d87317d7ca91 366 if (aux<70) value=1;
mbedalvaro 22:d87317d7ca91 367 else if (aux<80) value=2;
mbedalvaro 22:d87317d7ca91 368 else value=3;
mbedalvaro 24:4e52031a495b 369 */
mbedalvaro 22:d87317d7ca91 370
mbedalvaro 16:2ff1cb2ae1b1 371 for (int i=0; i<value ; i++) blobconf.addOneRigidLoopBouncing();
mbedalvaro 16:2ff1cb2ae1b1 372
mbedalvaro 16:2ff1cb2ae1b1 373 lsr.setConfigToRender(&blobconf);
mbedalvaro 16:2ff1cb2ae1b1 374 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 375 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 376 }
mbedalvaro 16:2ff1cb2ae1b1 377 }
mbedalvaro 16:2ff1cb2ae1b1 378
mbedalvaro 27:1ce994629ffc 379 else if (!strcmp(address[0], "spot_fountain")) {
mbedalvaro 27:1ce994629ffc 380 int value=data[0];
mbedalvaro 27:1ce994629ffc 381 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 27:1ce994629ffc 382
mbedalvaro 27:1ce994629ffc 383 timerForRendering.detach();
mbedalvaro 27:1ce994629ffc 384 // blobconf.computeBoundingBox();
mbedalvaro 27:1ce994629ffc 385 blobconf.clearConfig();
mbedalvaro 27:1ce994629ffc 386
mbedalvaro 27:1ce994629ffc 387 for (int i=0; i<value ; i++) blobconf.addOneRigidLoopFountain();
mbedalvaro 27:1ce994629ffc 388
mbedalvaro 27:1ce994629ffc 389 lsr.setConfigToRender(&blobconf);
mbedalvaro 27:1ce994629ffc 390 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 27:1ce994629ffc 391 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 27:1ce994629ffc 392 }
mbedalvaro 27:1ce994629ffc 393 }
mbedalvaro 28:44b7b6e35548 394
mbedalvaro 28:44b7b6e35548 395 else if (!strcmp(address[0], "air_hockey")) {
mbedalvaro 28:44b7b6e35548 396 int value=data[0];
mbedalvaro 28:44b7b6e35548 397 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 28:44b7b6e35548 398
mbedalvaro 28:44b7b6e35548 399 timerForRendering.detach();
mbedalvaro 28:44b7b6e35548 400 // blobconf.computeBoundingBox();
mbedalvaro 28:44b7b6e35548 401 blobconf.clearConfig();
mbedalvaro 28:44b7b6e35548 402
mbedalvaro 28:44b7b6e35548 403 for (int i=0; i<value ; i++) blobconf.addOneRigidLoopAirHockey();
mbedalvaro 28:44b7b6e35548 404
mbedalvaro 28:44b7b6e35548 405 lsr.setConfigToRender(&blobconf);
mbedalvaro 28:44b7b6e35548 406 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 28:44b7b6e35548 407 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 28:44b7b6e35548 408 }
mbedalvaro 28:44b7b6e35548 409 }
mbedalvaro 28:44b7b6e35548 410
mbedalvaro 28:44b7b6e35548 411 else if (!strcmp(address[0], "spot_following")) {
mbedalvaro 16:2ff1cb2ae1b1 412 int value=data[0];
mbedalvaro 16:2ff1cb2ae1b1 413 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 16:2ff1cb2ae1b1 414
mbedalvaro 16:2ff1cb2ae1b1 415 timerForRendering.detach();
mbedalvaro 16:2ff1cb2ae1b1 416 // blobconf.computeBoundingBox();
mbedalvaro 16:2ff1cb2ae1b1 417 blobconf.clearConfig();
mbedalvaro 22:d87317d7ca91 418
mbedalvaro 22:d87317d7ca91 419 // HACK for the time being:
mbedalvaro 24:4e52031a495b 420 /*
mbedalvaro 22:d87317d7ca91 421 int aux=rand()%100;
mbedalvaro 22:d87317d7ca91 422 if (aux<70) value=1;
mbedalvaro 22:d87317d7ca91 423 else if (aux<80) value=2;
mbedalvaro 22:d87317d7ca91 424 else value=6;
mbedalvaro 24:4e52031a495b 425 */
mbedalvaro 22:d87317d7ca91 426
mbedalvaro 16:2ff1cb2ae1b1 427 for (int i=0; i<value ; i++) blobconf.addOneRigidLoopFollowing();
mbedalvaro 22:d87317d7ca91 428
mbedalvaro 22:d87317d7ca91 429
mbedalvaro 16:2ff1cb2ae1b1 430 lsr.setConfigToRender(&blobconf);
mbedalvaro 16:2ff1cb2ae1b1 431 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 432 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 433 }
mbedalvaro 16:2ff1cb2ae1b1 434 }
mbedalvaro 16:2ff1cb2ae1b1 435
mbedalvaro 16:2ff1cb2ae1b1 436 else if (!strcmp(address[0], "spot_test")) {
mbedalvaro 0:345b3bc7a0ea 437 timerForRendering.detach();
mbedalvaro 0:345b3bc7a0ea 438 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 439 blobconf.clearConfig();
mbedalvaro 16:2ff1cb2ae1b1 440 blobconf.addOneRigidLoopTest();
mbedalvaro 1:a4050fee11f7 441 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 442 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 443 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 444 }
mbedalvaro 1:a4050fee11f7 445
mbedalvaro 1:a4050fee11f7 446 // other:
mbedalvaro 1:a4050fee11f7 447
mbedalvaro 24:4e52031a495b 448 else if ( !strcmp(address[0], "standby" ) ) { // will put ALL the blobs in stand by mode (no update function)
mbedalvaro 0:345b3bc7a0ea 449 blobconf.allStandBy(); // will avoid the update function
mbedalvaro 24:4e52031a495b 450 }
mbedalvaro 24:4e52031a495b 451 else if ( !strcmp(address[0], "resume" ) ) {
mbedalvaro 24:4e52031a495b 452 blobconf.allResume(); // Update function is called for all the blobs
mbedalvaro 1:a4050fee11f7 453 }
mbedalvaro 1:a4050fee11f7 454
mbedalvaro 1:a4050fee11f7 455 // (III) ========================================= Loop control (parameters, etc) ===========================================
mbedalvaro 19:228430f1350e 456
mbedalvaro 24:4e52031a495b 457 else if (!strcmp( address[0], "speedFactor" ) ) {
mbedalvaro 24:4e52031a495b 458 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 459 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 460 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 24:4e52031a495b 461 //if ((!strcmp(blobconf.blobArray[i].spotName, "rigid_following"))||(!strcmp(blobconf.blobArray[i].spotName, "rigid_following"))) {
mbedalvaro 24:4e52031a495b 462 blobconf.blobArray[i]->speedFactor((float)(1.0*value/100.0));
mbedalvaro 24:4e52031a495b 463 }
mbedalvaro 24:4e52031a495b 464 }
mbedalvaro 24:4e52031a495b 465 }
mbedalvaro 24:4e52031a495b 466
mbedalvaro 24:4e52031a495b 467 else if (!strcmp( address[0], "adjustPlusAngle" ) ) {
mbedalvaro 24:4e52031a495b 468 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 469 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 470 for (int i=0; i< blobconf.numBlobs; i++) blobconf.blobArray[i]->displaySensingBuffer.addDelayMirrors(value);
mbedalvaro 24:4e52031a495b 471 }
mbedalvaro 24:4e52031a495b 472 }
mbedalvaro 24:4e52031a495b 473
mbedalvaro 24:4e52031a495b 474 else if (!strcmp( address[0], "adjustMinusAngle" ) ) {
mbedalvaro 24:4e52031a495b 475 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 476 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 477 for (int i=0; i< blobconf.numBlobs; i++) blobconf.blobArray[i]->displaySensingBuffer.addDelayMirrors(-value);
mbedalvaro 24:4e52031a495b 478 }
mbedalvaro 24:4e52031a495b 479 }
mbedalvaro 24:4e52031a495b 480
mbedalvaro 1:a4050fee11f7 481 else if (!strcmp( address[0], "sendOSC" ) ) {
mbedalvaro 1:a4050fee11f7 482 int value=data[0];
mbedalvaro 1:a4050fee11f7 483 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 484 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 485 blobconf.blobArray[i]->sendOSC=(value>0);
mbedalvaro 1:a4050fee11f7 486 }
mbedalvaro 1:a4050fee11f7 487 }
mbedalvaro 1:a4050fee11f7 488 }
mbedalvaro 1:a4050fee11f7 489
mbedalvaro 1:a4050fee11f7 490 else if (!strcmp( address[0], "sendArea" ) ) {
mbedalvaro 1:a4050fee11f7 491 int value=data[0];
mbedalvaro 1:a4050fee11f7 492 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 493 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 494 blobconf.blobArray[i]->sendingBlobArea=(value>0);
mbedalvaro 1:a4050fee11f7 495 }
mbedalvaro 1:a4050fee11f7 496 }
mbedalvaro 1:a4050fee11f7 497 }
mbedalvaro 1:a4050fee11f7 498
mbedalvaro 1:a4050fee11f7 499 else if (!strcmp( address[0], "sendPos" ) ) {
mbedalvaro 1:a4050fee11f7 500 int value=data[0];
mbedalvaro 1:a4050fee11f7 501 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 502 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 503 blobconf.blobArray[i]->sendingLoopPositions=(value>0);
mbedalvaro 1:a4050fee11f7 504 }
mbedalvaro 1:a4050fee11f7 505 }
mbedalvaro 1:a4050fee11f7 506 }
mbedalvaro 1:a4050fee11f7 507
mbedalvaro 1:a4050fee11f7 508 else if (!strcmp( address[0], "sendRegions" ) ) {
mbedalvaro 1:a4050fee11f7 509 int value=data[0];
mbedalvaro 1:a4050fee11f7 510 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 511 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 512 blobconf.blobArray[i]->sendingLoopRegions=(value>0);
mbedalvaro 1:a4050fee11f7 513 }
mbedalvaro 1:a4050fee11f7 514 }
mbedalvaro 1:a4050fee11f7 515 }
mbedalvaro 1:a4050fee11f7 516
mbedalvaro 1:a4050fee11f7 517 else if (!strcmp( address[0], "sendTouched" ) ) {
mbedalvaro 1:a4050fee11f7 518 int value=data[0];
mbedalvaro 1:a4050fee11f7 519 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 520 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 521 blobconf.blobArray[i]->sendingTouched=(value>0);
mbedalvaro 1:a4050fee11f7 522 }
mbedalvaro 1:a4050fee11f7 523 }
mbedalvaro 1:a4050fee11f7 524 }
mbedalvaro 1:a4050fee11f7 525
mbedalvaro 1:a4050fee11f7 526
mbedalvaro 1:a4050fee11f7 527
mbedalvaro 0:345b3bc7a0ea 528 }
mbedalvaro 0:345b3bc7a0ea 529
mbedalvaro 0:345b3bc7a0ea 530 //============= RECEIVE OSC COMMANDS =========================
mbedalvaro 1:a4050fee11f7 531 // This is the callback function called when there are packets on the listening socket. It is not nice to have it
mbedalvaro 1:a4050fee11f7 532 // here, but for the time being having a "wrapping global" is the simplest solution (we cannot pass a member-function pointer
mbedalvaro 1:a4050fee11f7 533 // as handler to the upd object).
mbedalvaro 0:345b3bc7a0ea 534 void processOSC(UDPSocketEvent e) {
mbedalvaro 0:345b3bc7a0ea 535 osc.onUDPSocketEvent(e);
mbedalvaro 0:345b3bc7a0ea 536
mbedalvaro 0:345b3bc7a0ea 537 if (osc.newMessage) {
mbedalvaro 1:a4050fee11f7 538 // in fact, there is no need to check this if using the method of a global callback function - it is clear this is a new packet... however, it may be
mbedalvaro 1:a4050fee11f7 539 // interesting to use a timer, and process data (answers, etc) only after a certain amount of time, so as to avoid blocking the program in IRQ context...
mbedalvaro 1:a4050fee11f7 540
mbedalvaro 1:a4050fee11f7 541 // Acquire the addresses and arguments and put them in the GLOBAL variables:
mbedalvaro 1:a4050fee11f7 542 strcpy(address[0],"");
mbedalvaro 1:a4050fee11f7 543 strcpy(address[1],"");
mbedalvaro 1:a4050fee11f7 544 for (int i=0; i<recMes.getAddressNum(); i++) strcpy(address[i],recMes.getAddress(i)); // NOTE: up to the rest of the program to check if address[1] is really not null
mbedalvaro 1:a4050fee11f7 545 // Acquire data:
mbedalvaro 1:a4050fee11f7 546 data[0]=-1;
mbedalvaro 1:a4050fee11f7 547 data[1]=-1;
mbedalvaro 1:a4050fee11f7 548 for (int i=0; i<recMes.getArgNum(); i++) data[i]=(int)recMes.getArgInt(i);
mbedalvaro 1:a4050fee11f7 549
mbedalvaro 1:a4050fee11f7 550 // Finally, interpret the command:
mbedalvaro 1:a4050fee11f7 551 interpretCommand();//address, data);
mbedalvaro 1:a4050fee11f7 552
mbedalvaro 0:345b3bc7a0ea 553 }
mbedalvaro 1:a4050fee11f7 554 }
mbedalvaro 1:a4050fee11f7 555
mbedalvaro 0:345b3bc7a0ea 556 //============= RECEIVE SERIAL COMMANDS =========================
mbedalvaro 0:345b3bc7a0ea 557 //
mbedalvaro 1:a4050fee11f7 558 // NOTE: - NUMERIC PARAMETERS have to be send BEFORE the command word. They must be sent as ASCII DEC, without end character.
mbedalvaro 0:345b3bc7a0ea 559 // - Commands words SHOULD NOT have numbers in it. They should be C compliant STRINGS (ended with character '0')
mbedalvaro 1:a4050fee11f7 560 // - order is irrelevant: we can send 10 RADIUS or RADIUS 10.
mbedalvaro 0:345b3bc7a0ea 561
mbedalvaro 1:a4050fee11f7 562 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
mbedalvaro 0:345b3bc7a0ea 563 char stringData[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
mbedalvaro 0:345b3bc7a0ea 564 int indexStringData=0;//position of the byte in the string
mbedalvaro 0:345b3bc7a0ea 565
mbedalvaro 0:345b3bc7a0ea 566 // String to store COMMAND WORDS:
mbedalvaro 0:345b3bc7a0ea 567 char stringCommand[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
mbedalvaro 0:345b3bc7a0ea 568 int indexStringCommand=0;
mbedalvaro 0:345b3bc7a0ea 569 bool commandReady=false; // will become true when receiving the byte 0 (i.e. the '/0' string terminator)
mbedalvaro 0:345b3bc7a0ea 570
mbedalvaro 0:345b3bc7a0ea 571 void processSerial() {
mbedalvaro 0:345b3bc7a0ea 572
mbedalvaro 1:a4050fee11f7 573 while (pc.readable()>0) {
mbedalvaro 1:a4050fee11f7 574
mbedalvaro 14:0fc33a3a7b4b 575
mbedalvaro 1:a4050fee11f7 576 char val =pc.getc();
mbedalvaro 0:345b3bc7a0ea 577 // pc.printf("Got :%d\n", incomingByte);
mbedalvaro 1:a4050fee11f7 578 //pc.putc(incomingByte);
mbedalvaro 1:a4050fee11f7 579
mbedalvaro 1:a4050fee11f7 580 // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
mbedalvaro 1:a4050fee11f7 581 if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included)
mbedalvaro 1:a4050fee11f7 582 stringData[indexStringData] = val;
mbedalvaro 1:a4050fee11f7 583 indexStringData++;
mbedalvaro 1:a4050fee11f7 584 }
mbedalvaro 1:a4050fee11f7 585
mbedalvaro 1:a4050fee11f7 586 // Save ASCII letters in stringCommand:
mbedalvaro 1:a4050fee11f7 587 if ((val >= 'A') && (val <= 'z')) { // this is 65 to 122 (included)
mbedalvaro 1:a4050fee11f7 588 stringCommand[indexStringCommand] = val;
mbedalvaro 1:a4050fee11f7 589 indexStringCommand++;
mbedalvaro 1:a4050fee11f7 590 }
mbedalvaro 1:a4050fee11f7 591 // is command ready?
mbedalvaro 1:a4050fee11f7 592 if (val=='/') {
mbedalvaro 1:a4050fee11f7 593 commandReady=true;
mbedalvaro 1:a4050fee11f7 594 stringCommand[indexStringCommand] = 0; // string termination.
mbedalvaro 1:a4050fee11f7 595 indexStringCommand=0; // reset index string for acquiring next command
mbedalvaro 1:a4050fee11f7 596 //Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 597 }
mbedalvaro 1:a4050fee11f7 598
mbedalvaro 1:a4050fee11f7 599 // COMMANDS (with or without numeric parameters):
mbedalvaro 1:a4050fee11f7 600 if (commandReady==true) { // it means we can interpret the command string:
mbedalvaro 1:a4050fee11f7 601 commandReady=false;
mbedalvaro 1:a4050fee11f7 602
mbedalvaro 1:a4050fee11f7 603 stringData[indexStringData] = 0 ;// string termination for numeric values;
mbedalvaro 1:a4050fee11f7 604 indexStringData=0;
mbedalvaro 1:a4050fee11f7 605
mbedalvaro 1:a4050fee11f7 606 // PARSE DATA: (TO DO!!!!!!!!!!!!!!):
mbedalvaro 0:345b3bc7a0ea 607
mbedalvaro 1:a4050fee11f7 608 // (a) Parse command (get address[0] and address[1]):
mbedalvaro 1:a4050fee11f7 609 //ex: "/1/standBy" -- > address[0]="1" and address[1]="standBy"
mbedalvaro 1:a4050fee11f7 610 // address[2]
mbedalvaro 1:a4050fee11f7 611
mbedalvaro 1:a4050fee11f7 612 // Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 613 // Serial.println(stringData);
mbedalvaro 1:a4050fee11f7 614
mbedalvaro 1:a4050fee11f7 615 // (b) Parse data:
mbedalvaro 1:a4050fee11f7 616
mbedalvaro 1:a4050fee11f7 617 // char address[2][24];
mbedalvaro 1:a4050fee11f7 618 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 619 //int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 620
mbedalvaro 1:a4050fee11f7 621 // FOR THE TIME BEING there is no parsing for serial commands:
mbedalvaro 1:a4050fee11f7 622
mbedalvaro 1:a4050fee11f7 623 // SCANNING:
mbedalvaro 1:a4050fee11f7 624 if (!strcmp(stringCommand , "takeSnapshot")) {
mbedalvaro 1:a4050fee11f7 625 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 626 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 627
mbedalvaro 1:a4050fee11f7 628 // Then, do the scan (sending values on serial port):
mbedalvaro 24:4e52031a495b 629 IO.scan_serial(atoi(stringData));
mbedalvaro 1:a4050fee11f7 630
mbedalvaro 1:a4050fee11f7 631 // Finally, start again threaded display:
mbedalvaro 4:f9d364f10335 632 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 633 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 634 } else if (!strcmp(stringCommand , "REDON")) IO.setRedPower(1); // pc.printf("%d\n",incomingByte);
mbedalvaro 1:a4050fee11f7 635
mbedalvaro 14:0fc33a3a7b4b 636 else if (!strcmp(stringCommand , "REDOFF")) IO.setRedPower(0);
mbedalvaro 14:0fc33a3a7b4b 637
mbedalvaro 14:0fc33a3a7b4b 638 else if (!strcmp(stringCommand , "READVALUE")) pc.printf("Value read: %f", lockin.getSmoothValue());//lockin.getLastValue());/
mbedalvaro 1:a4050fee11f7 639
mbedalvaro 14:0fc33a3a7b4b 640 else if (!strcmp(stringCommand , "mbedReset")) mbed_reset();
mbedalvaro 16:2ff1cb2ae1b1 641
mbedalvaro 14:0fc33a3a7b4b 642 else if (!strcmp(stringCommand , "calibrate")) {
mbedalvaro 16:2ff1cb2ae1b1 643 // First, we need to disable the threaded display for the loop:
mbedalvaro 16:2ff1cb2ae1b1 644 timerForRendering.detach();
mbedalvaro 16:2ff1cb2ae1b1 645 // RESCAN (and save LUT table):
mbedalvaro 16:2ff1cb2ae1b1 646 IO.scanLUT();
mbedalvaro 16:2ff1cb2ae1b1 647 // Finally, start again threaded display:
mbedalvaro 16:2ff1cb2ae1b1 648 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 649 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 650 }
mbedalvaro 1:a4050fee11f7 651
mbedalvaro 1:a4050fee11f7 652 // FINALLY, interpret commands (but only after parsing):
mbedalvaro 1:a4050fee11f7 653 // interpretCommand();//address, data);
mbedalvaro 0:345b3bc7a0ea 654
mbedalvaro 0:345b3bc7a0ea 655 }
mbedalvaro 1:a4050fee11f7 656 }
mbedalvaro 1:a4050fee11f7 657 }
mbedalvaro 0:345b3bc7a0ea 658
mbedalvaro 0:345b3bc7a0ea 659
mbedalvaro 0:345b3bc7a0ea 660
mbedalvaro 0:345b3bc7a0ea 661 // ================ MISCELANEA
mbedalvaro 0:345b3bc7a0ea 662
mbedalvaro 0:345b3bc7a0ea 663 /* EXAMPLE SEND/RECEIVE on PROCESSING:
mbedalvaro 0:345b3bc7a0ea 664
mbedalvaro 0:345b3bc7a0ea 665 // oscP5sendreceive by andreas schlegel
mbedalvaro 0:345b3bc7a0ea 666 // example shows how to send and receive osc messages.
mbedalvaro 0:345b3bc7a0ea 667 // oscP5 website at http://www.sojamo.de/oscP5
mbedalvaro 0:345b3bc7a0ea 668
mbedalvaro 0:345b3bc7a0ea 669 import oscP5.*;
mbedalvaro 0:345b3bc7a0ea 670 import netP5.*;
mbedalvaro 1:a4050fee11f7 671
mbedalvaro 0:345b3bc7a0ea 672 OscP5 oscP5;
mbedalvaro 0:345b3bc7a0ea 673 NetAddress myRemoteLocation;
mbedalvaro 0:345b3bc7a0ea 674
mbedalvaro 0:345b3bc7a0ea 675 void setup() {
mbedalvaro 0:345b3bc7a0ea 676 size(400,400);
mbedalvaro 0:345b3bc7a0ea 677 frameRate(25);
mbedalvaro 1:a4050fee11f7 678 // start oscP5, listening for incoming messages at port 12000
mbedalvaro 0:345b3bc7a0ea 679 oscP5 = new OscP5(this,12000);
mbedalvaro 1:a4050fee11f7 680
mbedalvaro 0:345b3bc7a0ea 681 // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
mbedalvaro 0:345b3bc7a0ea 682 // an ip address and a port number. myRemoteLocation is used as parameter in
mbedalvaro 1:a4050fee11f7 683 // oscP5.send() when sending osc packets to another computer, device,
mbedalvaro 0:345b3bc7a0ea 684 // application. usage see below. for testing purposes the listening port
mbedalvaro 0:345b3bc7a0ea 685 // and the port of the remote location address are the same, hence you will
mbedalvaro 0:345b3bc7a0ea 686 // send messages back to this sketch.
mbedalvaro 0:345b3bc7a0ea 687 myRemoteLocation = new NetAddress("10.0.0.2",10000);
mbedalvaro 0:345b3bc7a0ea 688 }
mbedalvaro 0:345b3bc7a0ea 689
mbedalvaro 0:345b3bc7a0ea 690
mbedalvaro 0:345b3bc7a0ea 691 void draw() {
mbedalvaro 1:a4050fee11f7 692 background(0);
mbedalvaro 0:345b3bc7a0ea 693 }
mbedalvaro 0:345b3bc7a0ea 694
mbedalvaro 0:345b3bc7a0ea 695 void mousePressed() {
mbedalvaro 1:a4050fee11f7 696 // in the following different ways of creating osc messages are shown by example
mbedalvaro 0:345b3bc7a0ea 697 OscMessage myMessage = new OscMessage("/mbed/test1");
mbedalvaro 1:a4050fee11f7 698
mbedalvaro 1:a4050fee11f7 699 myMessage.add(123); // add an int to the osc message
mbedalvaro 0:345b3bc7a0ea 700
mbedalvaro 1:a4050fee11f7 701 // send the message
mbedalvaro 1:a4050fee11f7 702 oscP5.send(myMessage, myRemoteLocation);
mbedalvaro 0:345b3bc7a0ea 703 }
mbedalvaro 0:345b3bc7a0ea 704
mbedalvaro 0:345b3bc7a0ea 705
mbedalvaro 1:a4050fee11f7 706 // incoming osc message are forwarded to the oscEvent method.
mbedalvaro 0:345b3bc7a0ea 707 void oscEvent(OscMessage theOscMessage) {
mbedalvaro 1:a4050fee11f7 708 // print the address pattern and the typetag of the received OscMessage
mbedalvaro 0:345b3bc7a0ea 709 print("### received an osc message.");
mbedalvaro 0:345b3bc7a0ea 710 print(" addrpattern: "+theOscMessage.addrPattern());
mbedalvaro 0:345b3bc7a0ea 711 println(" typetag: "+theOscMessage.typetag());
mbedalvaro 0:345b3bc7a0ea 712 }
mbedalvaro 0:345b3bc7a0ea 713
mbedalvaro 0:345b3bc7a0ea 714 */