Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Wed Apr 11 13:06:23 2012 +0000
Revision:
9:3321170d157c
Parent:
8:5816bb17536b
Child:
10:6f8e48dca1bd
I will use an unsigned short to store the intensity instead of a float

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 0:345b3bc7a0ea 15 //DigitalOut myled3(LED2);
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 9:3321170d157c 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 1:a4050fee11f7 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 0:345b3bc7a0ea 41 uint8_t destIp[] = { 10, 0, 0, 1};
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 2:34157ebbf56b 63 Timer measureSendPeriod;
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 9:3321170d157c 108 blobconf.addOneElasticLoopContractCentral();
mbedalvaro 5:73cd58b58f95 109 // blobconf.addOneElasticContourFollowing();
mbedalvaro 1:a4050fee11f7 110
mbedalvaro 9:3321170d157c 111 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 9:3321170d157c 112 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 9:3321170d157c 113 //blobconf.addOneRigidLoopFollowing();
mbedalvaro 2:34157ebbf56b 114
mbedalvaro 7:0df17f3078bc 115
mbedalvaro 5:73cd58b58f95 116 //blobconf.addOneRigidLoopTest();
mbedalvaro 1:a4050fee11f7 117
mbedalvaro 0:345b3bc7a0ea 118 // Important: first, set the initial position for all the blobs, this will be useful because
mbedalvaro 0:345b3bc7a0ea 119 // when changing modes we can use the previous central position...
mbedalvaro 1:a4050fee11f7 120 // blobconf.setInitialPos(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_Y);
mbedalvaro 1:a4050fee11f7 121
mbedalvaro 5:73cd58b58f95 122 // draw the config once befor activating the laser buffer:
mbedalvaro 5:73cd58b58f95 123 blobconf.draw();
mbedalvaro 1:a4050fee11f7 124
mbedalvaro 0:345b3bc7a0ea 125 // 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 126 lsr.setConfigToRender(&blobconf);
mbedalvaro 1:a4050fee11f7 127
mbedalvaro 1:a4050fee11f7 128 // Timer on the rendering function of the oneLoop object:
mbedalvaro 9:3321170d157c 129 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 9:3321170d157c 130 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 1:a4050fee11f7 131
mbedalvaro 1:a4050fee11f7 132 // Timer for sending OSC data:
mbedalvaro 9:3321170d157c 133 // timerForSendingData.attach(&blobconf, &blobConfig::sendConfData, 0.025); // time in seconds (25ms -> 40Hz)
mbedalvaro 1:a4050fee11f7 134
mbedalvaro 1:a4050fee11f7 135 //========================================== INFINITE LOOP (in USER PROGRAM CONTEXT) ===================================================================
mbedalvaro 1:a4050fee11f7 136 #ifdef LOOPTIMECOMPUTE
mbedalvaro 1:a4050fee11f7 137 int timeCounterNum=1000;
mbedalvaro 1:a4050fee11f7 138 #endif
mbedalvaro 1:a4050fee11f7 139
mbedalvaro 9:3321170d157c 140 //measureUpdatePeriod.start();
mbedalvaro 9:3321170d157c 141 measureSendPeriod.start();
mbedalvaro 1:a4050fee11f7 142
mbedalvaro 1:a4050fee11f7 143 while (true) {
mbedalvaro 1:a4050fee11f7 144
mbedalvaro 7:0df17f3078bc 145 //NOTE: the updating period needs to be calculated as a function of the number of diplays points and the "refresh" rate of the laser.
mbedalvaro 7:0df17f3078bc 146 // THIS IS THE EQUICALENT OF "VERTICAL SYNCH"...
mbedalvaro 9:3321170d157c 147 // if (measureUpdatePeriod.read_ms()>4) { // 4 or 5 ms seems to be the minimum time required for performing one main loop (for a blob of 40 points)
mbedalvaro 9:3321170d157c 148 // with laser rendering ISR every 110us (total loop time 4300us, 3100us of effective loop time, and each laser interrupt about 30us)
mbedalvaro 9:3321170d157c 149 if (lsr.endedDisplay) {
mbedalvaro 9:3321170d157c 150
mbedalvaro 9:3321170d157c 151 lsr.endedDisplay=false;
mbedalvaro 9:3321170d157c 152
mbedalvaro 9:3321170d157c 153 // measureUpdatePeriod.stop();
mbedalvaro 9:3321170d157c 154 // measureUpdatePeriod.reset();
mbedalvaro 9:3321170d157c 155
mbedalvaro 9:3321170d157c 156 // __disable_irq();
mbedalvaro 9:3321170d157c 157
mbedalvaro 1:a4050fee11f7 158 // update config dynamics (this also could be threaded?):
mbedalvaro 1:a4050fee11f7 159 blobconf.update();
mbedalvaro 1:a4050fee11f7 160
mbedalvaro 1:a4050fee11f7 161 // draw the config (note: each kind of blob renders differently)
mbedalvaro 1:a4050fee11f7 162 blobconf.draw();
mbedalvaro 9:3321170d157c 163
mbedalvaro 9:3321170d157c 164 // __enable_irq();
mbedalvaro 9:3321170d157c 165
mbedalvaro 9:3321170d157c 166 // measureUpdatePeriod.start();
mbedalvaro 1:a4050fee11f7 167 }
mbedalvaro 0:345b3bc7a0ea 168
mbedalvaro 2:34157ebbf56b 169
mbedalvaro 1:a4050fee11f7 170 // COMMUNICATION:
mbedalvaro 1:a4050fee11f7 171 // (a) Reading commands:
mbedalvaro 1:a4050fee11f7 172 // Ethernet:
mbedalvaro 1:a4050fee11f7 173 Net::poll(); // this will take care of calling processOSC(UDPSocketEvent e) when a new packet arrives.
mbedalvaro 1:a4050fee11f7 174
mbedalvaro 1:a4050fee11f7 175 // Serial:
mbedalvaro 1:a4050fee11f7 176 #ifdef SERIAL_COMMANDS
mbedalvaro 1:a4050fee11f7 177 if (pc.readable()>0) processSerial();
mbedalvaro 1:a4050fee11f7 178 #endif
mbedalvaro 9:3321170d157c 179
mbedalvaro 2:34157ebbf56b 180 // (b)Sending Data: // PUT THIS IN AN INTERRUPT OR USE A TIMER!!! it may be TOO FAST...
mbedalvaro 2:34157ebbf56b 181 // 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 2:34157ebbf56b 182 if (measureSendPeriod.read_ms()>25) {
mbedalvaro 2:34157ebbf56b 183 measureSendPeriod.stop();
mbedalvaro 2:34157ebbf56b 184 measureSendPeriod.reset();
mbedalvaro 9:3321170d157c 185
mbedalvaro 2:34157ebbf56b 186 blobconf.sendConfData();
mbedalvaro 9:3321170d157c 187
mbedalvaro 9:3321170d157c 188 measureSendPeriod.start();
mbedalvaro 2:34157ebbf56b 189 }
mbedalvaro 1:a4050fee11f7 190
mbedalvaro 1:a4050fee11f7 191 // text:
mbedalvaro 1:a4050fee11f7 192 /*
mbedalvaro 1:a4050fee11f7 193 sendMes.setTopAddress("/hello");
mbedalvaro 1:a4050fee11f7 194 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 195 int x=(long)10;
mbedalvaro 1:a4050fee11f7 196 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 197 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 198 */
mbedalvaro 1:a4050fee11f7 199
mbedalvaro 0:345b3bc7a0ea 200 #ifdef LOOPTIMECOMPUTE
mbedalvaro 9:3321170d157c 201 if (timeCounterNum>50) myled = 0;
mbedalvaro 9:3321170d157c 202 // if (timeCounterNum%10==0) blobconf.sendConfData();
mbedalvaro 9:3321170d157c 203 if (timeCounterNum>100) {
mbedalvaro 1:a4050fee11f7 204 myled = 1;
mbedalvaro 1:a4050fee11f7 205 measureLoopPeriod.stop();
mbedalvaro 1:a4050fee11f7 206 sendMes.setTopAddress("/timeloop");
mbedalvaro 1:a4050fee11f7 207 sendMes.setSubAddress("/");
mbedalvaro 9:3321170d157c 208 // long x=(long)(int(measureLoopPeriod.read_us()/100.0));
mbedalvaro 9:3321170d157c 209 // long x=(long)(blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory.size());
mbedalvaro 9:3321170d157c 210 // long x=(long)(blobconf.blobArray[0]->normRecenteringVector);
mbedalvaro 9:3321170d157c 211 long x=(long)(1000*blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory[0].intensity);
mbedalvaro 1:a4050fee11f7 212 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 213 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 214 timeCounterNum=0;
mbedalvaro 1:a4050fee11f7 215 measureLoopPeriod.reset();
mbedalvaro 1:a4050fee11f7 216 measureLoopPeriod.start();
mbedalvaro 1:a4050fee11f7 217 } else timeCounterNum++;
mbedalvaro 0:345b3bc7a0ea 218 #endif
mbedalvaro 0:345b3bc7a0ea 219
mbedalvaro 0:345b3bc7a0ea 220 }
mbedalvaro 0:345b3bc7a0ea 221 }
mbedalvaro 0:345b3bc7a0ea 222
mbedalvaro 1:a4050fee11f7 223 // ================= INTERPRET COMMAND =========================
mbedalvaro 0:345b3bc7a0ea 224 // NOTE: the following arrays are GLOBAL (used in processOSC and processSerial, as well as in interpretCommand function):
mbedalvaro 1:a4050fee11f7 225 // max of two addresses (top and sub), of a max length of 24 characters:
mbedalvaro 0:345b3bc7a0ea 226 char address[2][24];
mbedalvaro 0:345b3bc7a0ea 227 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 228 int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 229
mbedalvaro 1:a4050fee11f7 230 //interpretCommand(const char& address[2][], const int& data[2]) {
mbedalvaro 0:345b3bc7a0ea 231 void interpretCommand() {
mbedalvaro 0:345b3bc7a0ea 232 // (I) =========================================== SPECIAL FUNCTIONS (reset, rescan LUT, etc) ====================================================
mbedalvaro 0:345b3bc7a0ea 233 if ( !strcmp(address[0], "takeSnapshot" ) ) { // will reset all the spots, but the 0, and use it for taking measures:
mbedalvaro 1:a4050fee11f7 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 1:a4050fee11f7 246 // Then, do the scan (sending values on serial port):
mbedalvaro 1:a4050fee11f7 247 IO.scan_serial();
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 1:a4050fee11f7 252 }
mbedalvaro 1:a4050fee11f7 253
mbedalvaro 1:a4050fee11f7 254 else if ( !strcmp(address[0], "mbedReset" ) ) mbed_reset();
mbedalvaro 1:a4050fee11f7 255
mbedalvaro 1:a4050fee11f7 256 else if ( !strcmp(address[0], "calibrate" ) ) {
mbedalvaro 1:a4050fee11f7 257 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 258 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 259 // RESCAN (and save LUT table):
mbedalvaro 1:a4050fee11f7 260 IO.scanLUT();
mbedalvaro 1:a4050fee11f7 261 // Finally, start again threaded display:
mbedalvaro 9:3321170d157c 262 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 263 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 264 }
mbedalvaro 1:a4050fee11f7 265
mbedalvaro 1:a4050fee11f7 266 // (II) ========================================= GLOBAL CONFIG and HARDWARE COMMANDS ===========================================
mbedalvaro 1:a4050fee11f7 267
mbedalvaro 1:a4050fee11f7 268 else if ( !strcmp(address[0], "setGreenLaser" ) ) {
mbedalvaro 1:a4050fee11f7 269 int value=data[0];
mbedalvaro 1:a4050fee11f7 270 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 271 if (value==0) IO.setGreenPower(0);
mbedalvaro 1:a4050fee11f7 272 else IO.setGreenPower(1);
mbedalvaro 1:a4050fee11f7 273 }
mbedalvaro 1:a4050fee11f7 274 }
mbedalvaro 1:a4050fee11f7 275
mbedalvaro 1:a4050fee11f7 276 // SIMPLE BEHAVIOUR MODES (to be read from an XML file in the future):
mbedalvaro 1:a4050fee11f7 277 else if (!strcmp(address[0], "crawling")) { //
mbedalvaro 1:a4050fee11f7 278 timerForRendering.detach();
mbedalvaro 0:345b3bc7a0ea 279 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 280 blobconf.clearConfig();
mbedalvaro 1:a4050fee11f7 281 blobconf.addOneElasticContourFollowing();
mbedalvaro 1:a4050fee11f7 282 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 283 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 284 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 285 } else if (!strcmp(address[0], "loop")) { //
mbedalvaro 1:a4050fee11f7 286 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 287 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 288 blobconf.clearConfig();
mbedalvaro 1:a4050fee11f7 289 blobconf.addOneElasticLoopContractCentral();
mbedalvaro 1:a4050fee11f7 290 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 291 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 292 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 293 }
mbedalvaro 1:a4050fee11f7 294
mbedalvaro 1:a4050fee11f7 295 else if (!strcmp(address[0], "bouncing")) {
mbedalvaro 0:345b3bc7a0ea 296 timerForRendering.detach();
mbedalvaro 0:345b3bc7a0ea 297 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 298 blobconf.clearConfig();
mbedalvaro 1:a4050fee11f7 299 blobconf.addOneElasticBouncing();
mbedalvaro 1:a4050fee11f7 300 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 301 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 302 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 303 }
mbedalvaro 1:a4050fee11f7 304
mbedalvaro 1:a4050fee11f7 305 // other:
mbedalvaro 1:a4050fee11f7 306
mbedalvaro 1:a4050fee11f7 307 else if ( !strcmp(address[0], "standby" ) ) { // will put ALL the spots in stand by mode
mbedalvaro 0:345b3bc7a0ea 308 blobconf.allStandBy(); // will avoid the update function
mbedalvaro 1:a4050fee11f7 309 } else if ( !strcmp(address[0], "resume" ) ) { // will put ALL the spots in stand by mode
mbedalvaro 0:345b3bc7a0ea 310 blobconf.allResume(); // will avoid the update function
mbedalvaro 1:a4050fee11f7 311 }
mbedalvaro 1:a4050fee11f7 312
mbedalvaro 1:a4050fee11f7 313 // (III) ========================================= Loop control (parameters, etc) ===========================================
mbedalvaro 1:a4050fee11f7 314 // NOte: for the time being, we only have ONE loop, so there is no "per loop or per config" mode.
mbedalvaro 1:a4050fee11f7 315
mbedalvaro 1:a4050fee11f7 316 else if (!strcmp( address[0], "sendOSC" ) ) {
mbedalvaro 1:a4050fee11f7 317 int value=data[0];
mbedalvaro 1:a4050fee11f7 318 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 319 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 320 blobconf.blobArray[i]->sendOSC=(value>0);
mbedalvaro 1:a4050fee11f7 321 }
mbedalvaro 1:a4050fee11f7 322 }
mbedalvaro 1:a4050fee11f7 323 }
mbedalvaro 1:a4050fee11f7 324
mbedalvaro 1:a4050fee11f7 325 else if (!strcmp( address[0], "sendArea" ) ) {
mbedalvaro 1:a4050fee11f7 326 int value=data[0];
mbedalvaro 1:a4050fee11f7 327 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 328 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 329 blobconf.blobArray[i]->sendingBlobArea=(value>0);
mbedalvaro 1:a4050fee11f7 330 }
mbedalvaro 1:a4050fee11f7 331 }
mbedalvaro 1:a4050fee11f7 332 }
mbedalvaro 1:a4050fee11f7 333
mbedalvaro 1:a4050fee11f7 334 else if (!strcmp( address[0], "sendPos" ) ) {
mbedalvaro 1:a4050fee11f7 335 int value=data[0];
mbedalvaro 1:a4050fee11f7 336 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 337 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 338 blobconf.blobArray[i]->sendingLoopPositions=(value>0);
mbedalvaro 1:a4050fee11f7 339 }
mbedalvaro 1:a4050fee11f7 340 }
mbedalvaro 1:a4050fee11f7 341 }
mbedalvaro 1:a4050fee11f7 342
mbedalvaro 1:a4050fee11f7 343 else if (!strcmp( address[0], "sendRegions" ) ) {
mbedalvaro 1:a4050fee11f7 344 int value=data[0];
mbedalvaro 1:a4050fee11f7 345 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 346 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 347 blobconf.blobArray[i]->sendingLoopRegions=(value>0);
mbedalvaro 1:a4050fee11f7 348 }
mbedalvaro 1:a4050fee11f7 349 }
mbedalvaro 1:a4050fee11f7 350 }
mbedalvaro 1:a4050fee11f7 351
mbedalvaro 1:a4050fee11f7 352 else if (!strcmp( address[0], "sendTouched" ) ) {
mbedalvaro 1:a4050fee11f7 353 int value=data[0];
mbedalvaro 1:a4050fee11f7 354 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 355 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 356 blobconf.blobArray[i]->sendingTouched=(value>0);
mbedalvaro 1:a4050fee11f7 357 }
mbedalvaro 1:a4050fee11f7 358 }
mbedalvaro 1:a4050fee11f7 359 }
mbedalvaro 1:a4050fee11f7 360
mbedalvaro 1:a4050fee11f7 361
mbedalvaro 1:a4050fee11f7 362
mbedalvaro 0:345b3bc7a0ea 363 }
mbedalvaro 0:345b3bc7a0ea 364
mbedalvaro 0:345b3bc7a0ea 365 //============= RECEIVE OSC COMMANDS =========================
mbedalvaro 1:a4050fee11f7 366 // This is the callback function called when there are packets on the listening socket. It is not nice to have it
mbedalvaro 1:a4050fee11f7 367 // here, but for the time being having a "wrapping global" is the simplest solution (we cannot pass a member-function pointer
mbedalvaro 1:a4050fee11f7 368 // as handler to the upd object).
mbedalvaro 0:345b3bc7a0ea 369 void processOSC(UDPSocketEvent e) {
mbedalvaro 0:345b3bc7a0ea 370 osc.onUDPSocketEvent(e);
mbedalvaro 0:345b3bc7a0ea 371
mbedalvaro 0:345b3bc7a0ea 372 if (osc.newMessage) {
mbedalvaro 1:a4050fee11f7 373 // 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 374 // 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 375
mbedalvaro 1:a4050fee11f7 376 // Acquire the addresses and arguments and put them in the GLOBAL variables:
mbedalvaro 1:a4050fee11f7 377 strcpy(address[0],"");
mbedalvaro 1:a4050fee11f7 378 strcpy(address[1],"");
mbedalvaro 1:a4050fee11f7 379 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 380 // Acquire data:
mbedalvaro 1:a4050fee11f7 381 data[0]=-1;
mbedalvaro 1:a4050fee11f7 382 data[1]=-1;
mbedalvaro 1:a4050fee11f7 383 for (int i=0; i<recMes.getArgNum(); i++) data[i]=(int)recMes.getArgInt(i);
mbedalvaro 1:a4050fee11f7 384
mbedalvaro 1:a4050fee11f7 385 // Finally, interpret the command:
mbedalvaro 1:a4050fee11f7 386 interpretCommand();//address, data);
mbedalvaro 1:a4050fee11f7 387
mbedalvaro 0:345b3bc7a0ea 388 }
mbedalvaro 1:a4050fee11f7 389 }
mbedalvaro 1:a4050fee11f7 390
mbedalvaro 0:345b3bc7a0ea 391 //============= RECEIVE SERIAL COMMANDS =========================
mbedalvaro 0:345b3bc7a0ea 392 //
mbedalvaro 1:a4050fee11f7 393 // NOTE: - NUMERIC PARAMETERS have to be send BEFORE the command word. They must be sent as ASCII DEC, without end character.
mbedalvaro 0:345b3bc7a0ea 394 // - Commands words SHOULD NOT have numbers in it. They should be C compliant STRINGS (ended with character '0')
mbedalvaro 1:a4050fee11f7 395 // - order is irrelevant: we can send 10 RADIUS or RADIUS 10.
mbedalvaro 0:345b3bc7a0ea 396
mbedalvaro 1:a4050fee11f7 397 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
mbedalvaro 0:345b3bc7a0ea 398 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 399 int indexStringData=0;//position of the byte in the string
mbedalvaro 0:345b3bc7a0ea 400
mbedalvaro 0:345b3bc7a0ea 401 // String to store COMMAND WORDS:
mbedalvaro 0:345b3bc7a0ea 402 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 403 int indexStringCommand=0;
mbedalvaro 0:345b3bc7a0ea 404 bool commandReady=false; // will become true when receiving the byte 0 (i.e. the '/0' string terminator)
mbedalvaro 0:345b3bc7a0ea 405
mbedalvaro 0:345b3bc7a0ea 406 void processSerial() {
mbedalvaro 0:345b3bc7a0ea 407
mbedalvaro 1:a4050fee11f7 408 while (pc.readable()>0) {
mbedalvaro 1:a4050fee11f7 409
mbedalvaro 1:a4050fee11f7 410 char val =pc.getc();
mbedalvaro 0:345b3bc7a0ea 411 // pc.printf("Got :%d\n", incomingByte);
mbedalvaro 1:a4050fee11f7 412 //pc.putc(incomingByte);
mbedalvaro 1:a4050fee11f7 413
mbedalvaro 1:a4050fee11f7 414 // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
mbedalvaro 1:a4050fee11f7 415 if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included)
mbedalvaro 1:a4050fee11f7 416 stringData[indexStringData] = val;
mbedalvaro 1:a4050fee11f7 417 indexStringData++;
mbedalvaro 1:a4050fee11f7 418 }
mbedalvaro 1:a4050fee11f7 419
mbedalvaro 1:a4050fee11f7 420 // Save ASCII letters in stringCommand:
mbedalvaro 1:a4050fee11f7 421 if ((val >= 'A') && (val <= 'z')) { // this is 65 to 122 (included)
mbedalvaro 1:a4050fee11f7 422 stringCommand[indexStringCommand] = val;
mbedalvaro 1:a4050fee11f7 423 indexStringCommand++;
mbedalvaro 1:a4050fee11f7 424 }
mbedalvaro 1:a4050fee11f7 425 // is command ready?
mbedalvaro 1:a4050fee11f7 426 if (val=='/') {
mbedalvaro 1:a4050fee11f7 427 commandReady=true;
mbedalvaro 1:a4050fee11f7 428 stringCommand[indexStringCommand] = 0; // string termination.
mbedalvaro 1:a4050fee11f7 429 indexStringCommand=0; // reset index string for acquiring next command
mbedalvaro 1:a4050fee11f7 430 //Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 431 }
mbedalvaro 1:a4050fee11f7 432
mbedalvaro 1:a4050fee11f7 433 // COMMANDS (with or without numeric parameters):
mbedalvaro 1:a4050fee11f7 434 if (commandReady==true) { // it means we can interpret the command string:
mbedalvaro 1:a4050fee11f7 435 commandReady=false;
mbedalvaro 1:a4050fee11f7 436
mbedalvaro 1:a4050fee11f7 437 stringData[indexStringData] = 0 ;// string termination for numeric values;
mbedalvaro 1:a4050fee11f7 438 indexStringData=0;
mbedalvaro 1:a4050fee11f7 439
mbedalvaro 1:a4050fee11f7 440 // PARSE DATA: (TO DO!!!!!!!!!!!!!!):
mbedalvaro 0:345b3bc7a0ea 441
mbedalvaro 1:a4050fee11f7 442 // (a) Parse command (get address[0] and address[1]):
mbedalvaro 1:a4050fee11f7 443 //ex: "/1/standBy" -- > address[0]="1" and address[1]="standBy"
mbedalvaro 1:a4050fee11f7 444 // address[2]
mbedalvaro 1:a4050fee11f7 445
mbedalvaro 1:a4050fee11f7 446 // Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 447 // Serial.println(stringData);
mbedalvaro 1:a4050fee11f7 448
mbedalvaro 1:a4050fee11f7 449 // (b) Parse data:
mbedalvaro 1:a4050fee11f7 450
mbedalvaro 1:a4050fee11f7 451 // char address[2][24];
mbedalvaro 1:a4050fee11f7 452 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 453 //int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 454
mbedalvaro 1:a4050fee11f7 455 // FOR THE TIME BEING there is no parsing for serial commands:
mbedalvaro 1:a4050fee11f7 456
mbedalvaro 1:a4050fee11f7 457 // SCANNING:
mbedalvaro 1:a4050fee11f7 458 if (!strcmp(stringCommand , "takeSnapshot")) {
mbedalvaro 1:a4050fee11f7 459 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 460 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 461
mbedalvaro 1:a4050fee11f7 462 // Then, do the scan (sending values on serial port):
mbedalvaro 1:a4050fee11f7 463 IO.scan_serial();
mbedalvaro 1:a4050fee11f7 464
mbedalvaro 1:a4050fee11f7 465 // Finally, start again threaded display:
mbedalvaro 4:f9d364f10335 466 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 467 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 468 }
mbedalvaro 1:a4050fee11f7 469
mbedalvaro 1:a4050fee11f7 470 if (!strcmp(stringCommand , "REDON")) IO.setRedPower(1); // pc.printf("%d\n",incomingByte);
mbedalvaro 1:a4050fee11f7 471
mbedalvaro 1:a4050fee11f7 472 if (!strcmp(stringCommand , "REDOFF")) IO.setRedPower(0);
mbedalvaro 1:a4050fee11f7 473
mbedalvaro 1:a4050fee11f7 474 if (!strcmp(stringCommand , "READVALUE")) pc.printf("Value read: %f", lockin.getSmoothValue());//lockin.getLastValue());/
mbedalvaro 1:a4050fee11f7 475
mbedalvaro 1:a4050fee11f7 476 // FINALLY, interpret commands (but only after parsing):
mbedalvaro 1:a4050fee11f7 477 // interpretCommand();//address, data);
mbedalvaro 0:345b3bc7a0ea 478
mbedalvaro 0:345b3bc7a0ea 479 }
mbedalvaro 1:a4050fee11f7 480 }
mbedalvaro 1:a4050fee11f7 481 }
mbedalvaro 0:345b3bc7a0ea 482
mbedalvaro 0:345b3bc7a0ea 483
mbedalvaro 0:345b3bc7a0ea 484
mbedalvaro 0:345b3bc7a0ea 485 // ================ MISCELANEA
mbedalvaro 0:345b3bc7a0ea 486
mbedalvaro 0:345b3bc7a0ea 487 /* EXAMPLE SEND/RECEIVE on PROCESSING:
mbedalvaro 0:345b3bc7a0ea 488
mbedalvaro 0:345b3bc7a0ea 489 // oscP5sendreceive by andreas schlegel
mbedalvaro 0:345b3bc7a0ea 490 // example shows how to send and receive osc messages.
mbedalvaro 0:345b3bc7a0ea 491 // oscP5 website at http://www.sojamo.de/oscP5
mbedalvaro 0:345b3bc7a0ea 492
mbedalvaro 0:345b3bc7a0ea 493 import oscP5.*;
mbedalvaro 0:345b3bc7a0ea 494 import netP5.*;
mbedalvaro 1:a4050fee11f7 495
mbedalvaro 0:345b3bc7a0ea 496 OscP5 oscP5;
mbedalvaro 0:345b3bc7a0ea 497 NetAddress myRemoteLocation;
mbedalvaro 0:345b3bc7a0ea 498
mbedalvaro 0:345b3bc7a0ea 499 void setup() {
mbedalvaro 0:345b3bc7a0ea 500 size(400,400);
mbedalvaro 0:345b3bc7a0ea 501 frameRate(25);
mbedalvaro 1:a4050fee11f7 502 // start oscP5, listening for incoming messages at port 12000
mbedalvaro 0:345b3bc7a0ea 503 oscP5 = new OscP5(this,12000);
mbedalvaro 1:a4050fee11f7 504
mbedalvaro 0:345b3bc7a0ea 505 // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
mbedalvaro 0:345b3bc7a0ea 506 // an ip address and a port number. myRemoteLocation is used as parameter in
mbedalvaro 1:a4050fee11f7 507 // oscP5.send() when sending osc packets to another computer, device,
mbedalvaro 0:345b3bc7a0ea 508 // application. usage see below. for testing purposes the listening port
mbedalvaro 0:345b3bc7a0ea 509 // and the port of the remote location address are the same, hence you will
mbedalvaro 0:345b3bc7a0ea 510 // send messages back to this sketch.
mbedalvaro 0:345b3bc7a0ea 511 myRemoteLocation = new NetAddress("10.0.0.2",10000);
mbedalvaro 0:345b3bc7a0ea 512 }
mbedalvaro 0:345b3bc7a0ea 513
mbedalvaro 0:345b3bc7a0ea 514
mbedalvaro 0:345b3bc7a0ea 515 void draw() {
mbedalvaro 1:a4050fee11f7 516 background(0);
mbedalvaro 0:345b3bc7a0ea 517 }
mbedalvaro 0:345b3bc7a0ea 518
mbedalvaro 0:345b3bc7a0ea 519 void mousePressed() {
mbedalvaro 1:a4050fee11f7 520 // in the following different ways of creating osc messages are shown by example
mbedalvaro 0:345b3bc7a0ea 521 OscMessage myMessage = new OscMessage("/mbed/test1");
mbedalvaro 1:a4050fee11f7 522
mbedalvaro 1:a4050fee11f7 523 myMessage.add(123); // add an int to the osc message
mbedalvaro 0:345b3bc7a0ea 524
mbedalvaro 1:a4050fee11f7 525 // send the message
mbedalvaro 1:a4050fee11f7 526 oscP5.send(myMessage, myRemoteLocation);
mbedalvaro 0:345b3bc7a0ea 527 }
mbedalvaro 0:345b3bc7a0ea 528
mbedalvaro 0:345b3bc7a0ea 529
mbedalvaro 1:a4050fee11f7 530 // incoming osc message are forwarded to the oscEvent method.
mbedalvaro 0:345b3bc7a0ea 531 void oscEvent(OscMessage theOscMessage) {
mbedalvaro 1:a4050fee11f7 532 // print the address pattern and the typetag of the received OscMessage
mbedalvaro 0:345b3bc7a0ea 533 print("### received an osc message.");
mbedalvaro 0:345b3bc7a0ea 534 print(" addrpattern: "+theOscMessage.addrPattern());
mbedalvaro 0:345b3bc7a0ea 535 println(" typetag: "+theOscMessage.typetag());
mbedalvaro 0:345b3bc7a0ea 536 }
mbedalvaro 0:345b3bc7a0ea 537
mbedalvaro 0:345b3bc7a0ea 538 */