just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Sat Mar 31 08:19:31 2012 +0000
Revision:
1:a4050fee11f7
Parent:
0:345b3bc7a0ea
Child:
2:34157ebbf56b
new scorelight methods added. VERY STRANGE problem with oldPos and pos in Verlet method, that have strange behaviour depending on the value of damp factor...

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