lalalala
Dependencies: mbed
main.cpp@0:af4f83b43162, 2014-12-10 (annotated)
- Committer:
- jstroud
- Date:
- Wed Dec 10 18:59:21 2014 +0000
- Revision:
- 0:af4f83b43162
force sensor code for processing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jstroud | 0:af4f83b43162 | 1 | #include "mbed.h" |
jstroud | 0:af4f83b43162 | 2 | |
jstroud | 0:af4f83b43162 | 3 | //serial connection to PC via USB |
jstroud | 0:af4f83b43162 | 4 | Serial pc(USBTX, USBRX); |
jstroud | 0:af4f83b43162 | 5 | AnalogIn strut1Force(P0_1); |
jstroud | 0:af4f83b43162 | 6 | AnalogIn strut2Force(P0_2); |
jstroud | 0:af4f83b43162 | 7 | AnalogIn strut3Force(P0_3); |
jstroud | 0:af4f83b43162 | 8 | AnalogIn strut4Force(P0_4); |
jstroud | 0:af4f83b43162 | 9 | AnalogIn seatForce(P0_5); |
jstroud | 0:af4f83b43162 | 10 | AnalogIn cupBottomForce(P0_6); |
jstroud | 0:af4f83b43162 | 11 | |
jstroud | 0:af4f83b43162 | 12 | DigitalOut light(LED1); |
jstroud | 0:af4f83b43162 | 13 | |
jstroud | 0:af4f83b43162 | 14 | int main(void) |
jstroud | 0:af4f83b43162 | 15 | { |
jstroud | 0:af4f83b43162 | 16 | pc.printf("init\n"); |
jstroud | 0:af4f83b43162 | 17 | while (true) { |
jstroud | 0:af4f83b43162 | 18 | //pc.printf("test"); |
jstroud | 0:af4f83b43162 | 19 | //light = 1; |
jstroud | 0:af4f83b43162 | 20 | float vals[6] = { strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read(), seatForce.read(), cupBottomForce.read() }; |
jstroud | 0:af4f83b43162 | 21 | pc.printf("%1.2f,%1.2f,%1.2f,%1.2f,%1.2f,%1.2f\n", vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]); |
jstroud | 0:af4f83b43162 | 22 | wait(0.001f); // wait 50ms (200Hz update rate) |
jstroud | 0:af4f83b43162 | 23 | //light = 0; |
jstroud | 0:af4f83b43162 | 24 | } |
jstroud | 0:af4f83b43162 | 25 | |
jstroud | 0:af4f83b43162 | 26 | } |
jstroud | 0:af4f83b43162 | 27 | /* // Sensor graphing sketch with threshold |
jstroud | 0:af4f83b43162 | 28 | // This program takes ASCII-encoded strings |
jstroud | 0:af4f83b43162 | 29 | // from the serial port at 9600 baud and graphs them. It expects float values in the |
jstroud | 0:af4f83b43162 | 30 | // range of -1.0 to 1.0, followed by a newline, or newline and carriage return |
jstroud | 0:af4f83b43162 | 31 | |
jstroud | 0:af4f83b43162 | 32 | // based on Tom Igoe's Arduino sensor graphing sketch, created 20 Apr 2005 |
jstroud | 0:af4f83b43162 | 33 | // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed |
jstroud | 0:af4f83b43162 | 34 | // This example code is in the public domain. |
jstroud | 0:af4f83b43162 | 35 | |
jstroud | 0:af4f83b43162 | 36 | import processing.serial.*; |
jstroud | 0:af4f83b43162 | 37 | import java.util.Scanner; |
jstroud | 0:af4f83b43162 | 38 | |
jstroud | 0:af4f83b43162 | 39 | Serial myPort; // The serial port |
jstroud | 0:af4f83b43162 | 40 | int xPos = 1; // horizontal position of the graph |
jstroud | 0:af4f83b43162 | 41 | float threshold=0.3; |
jstroud | 0:af4f83b43162 | 42 | float minThreshold = 0.1; |
jstroud | 0:af4f83b43162 | 43 | float max = 0; |
jstroud | 0:af4f83b43162 | 44 | boolean rise = true; |
jstroud | 0:af4f83b43162 | 45 | PImage bearImg; |
jstroud | 0:af4f83b43162 | 46 | |
jstroud | 0:af4f83b43162 | 47 | void setup () { |
jstroud | 0:af4f83b43162 | 48 | // set the window size: |
jstroud | 0:af4f83b43162 | 49 | //size(400, 300); |
jstroud | 0:af4f83b43162 | 50 | //fullscreen |
jstroud | 0:af4f83b43162 | 51 | size(displayWidth, displayHeight); |
jstroud | 0:af4f83b43162 | 52 | // List all the available serial ports to help you find the one you need |
jstroud | 0:af4f83b43162 | 53 | println(Serial.list()); |
jstroud | 0:af4f83b43162 | 54 | // Open whatever port is the one you're using. |
jstroud | 0:af4f83b43162 | 55 | myPort = new Serial(this, "/dev/tty.usbmodem1422", 9600); |
jstroud | 0:af4f83b43162 | 56 | // don't generate a serialEvent() unless you get a newline character: |
jstroud | 0:af4f83b43162 | 57 | myPort.bufferUntil('\n'); |
jstroud | 0:af4f83b43162 | 58 | // set inital background: |
jstroud | 0:af4f83b43162 | 59 | redrawBG(); |
jstroud | 0:af4f83b43162 | 60 | |
jstroud | 0:af4f83b43162 | 61 | bearImg = loadImage("cal_bear.png"); |
jstroud | 0:af4f83b43162 | 62 | } |
jstroud | 0:af4f83b43162 | 63 | |
jstroud | 0:af4f83b43162 | 64 | void draw () { |
jstroud | 0:af4f83b43162 | 65 | |
jstroud | 0:af4f83b43162 | 66 | // everything happens in the serialEvent() |
jstroud | 0:af4f83b43162 | 67 | } |
jstroud | 0:af4f83b43162 | 68 | |
jstroud | 0:af4f83b43162 | 69 | void keyPressed() { |
jstroud | 0:af4f83b43162 | 70 | if (key == CODED) { |
jstroud | 0:af4f83b43162 | 71 | if (keyCode == UP) { |
jstroud | 0:af4f83b43162 | 72 | threshold+=0.05; |
jstroud | 0:af4f83b43162 | 73 | } else if (keyCode == DOWN) { |
jstroud | 0:af4f83b43162 | 74 | threshold-=0.05; |
jstroud | 0:af4f83b43162 | 75 | } |
jstroud | 0:af4f83b43162 | 76 | redrawBG(); |
jstroud | 0:af4f83b43162 | 77 | } |
jstroud | 0:af4f83b43162 | 78 | } |
jstroud | 0:af4f83b43162 | 79 | |
jstroud | 0:af4f83b43162 | 80 | // threshold 0.2 - 0.9 |
jstroud | 0:af4f83b43162 | 81 | |
jstroud | 0:af4f83b43162 | 82 | void serialEvent (Serial myPort) { |
jstroud | 0:af4f83b43162 | 83 | // get the ASCII string: |
jstroud | 0:af4f83b43162 | 84 | |
jstroud | 0:af4f83b43162 | 85 | String inString = myPort.readStringUntil('\n'); |
jstroud | 0:af4f83b43162 | 86 | |
jstroud | 0:af4f83b43162 | 87 | if (inString != null) { |
jstroud | 0:af4f83b43162 | 88 | Scanner scanner = new Scanner(inString); |
jstroud | 0:af4f83b43162 | 89 | |
jstroud | 0:af4f83b43162 | 90 | // trim off any whitespace: |
jstroud | 0:af4f83b43162 | 91 | inString = trim(inString); |
jstroud | 0:af4f83b43162 | 92 | // convert to an int and map to the screen height: |
jstroud | 0:af4f83b43162 | 93 | float inFloat =0.f; |
jstroud | 0:af4f83b43162 | 94 | try{ |
jstroud | 0:af4f83b43162 | 95 | inFloat = scanner.nextFloat();//float(inString); |
jstroud | 0:af4f83b43162 | 96 | } catch (Exception e) { |
jstroud | 0:af4f83b43162 | 97 | print(e); |
jstroud | 0:af4f83b43162 | 98 | } |
jstroud | 0:af4f83b43162 | 99 | |
jstroud | 0:af4f83b43162 | 100 | |
jstroud | 0:af4f83b43162 | 101 | if(rise) { // we were last below |
jstroud | 0:af4f83b43162 | 102 | if(inFloat>threshold) { // we're above where we want to be |
jstroud | 0:af4f83b43162 | 103 | // rising edge, becoming true |
jstroud | 0:af4f83b43162 | 104 | rise = false; |
jstroud | 0:af4f83b43162 | 105 | drawHit(inFloat); |
jstroud | 0:af4f83b43162 | 106 | |
jstroud | 0:af4f83b43162 | 107 | } // else // do nothing |
jstroud | 0:af4f83b43162 | 108 | |
jstroud | 0:af4f83b43162 | 109 | } else { // below == false, above threshold |
jstroud | 0:af4f83b43162 | 110 | if(inFloat < threshold) { //we drop below threshold |
jstroud | 0:af4f83b43162 | 111 | rise = true; |
jstroud | 0:af4f83b43162 | 112 | delay(500); |
jstroud | 0:af4f83b43162 | 113 | redrawBG(); |
jstroud | 0:af4f83b43162 | 114 | max = 0; |
jstroud | 0:af4f83b43162 | 115 | } else // below == false, still above threshold |
jstroud | 0:af4f83b43162 | 116 | drawHit(inFloat); |
jstroud | 0:af4f83b43162 | 117 | } |
jstroud | 0:af4f83b43162 | 118 | |
jstroud | 0:af4f83b43162 | 119 | |
jstroud | 0:af4f83b43162 | 120 | // at the edge of the screen, go back to the beginning: |
jstroud | 0:af4f83b43162 | 121 | } |
jstroud | 0:af4f83b43162 | 122 | } |
jstroud | 0:af4f83b43162 | 123 | |
jstroud | 0:af4f83b43162 | 124 | void drawHit(float inFloat) { |
jstroud | 0:af4f83b43162 | 125 | if(max < inFloat) { |
jstroud | 0:af4f83b43162 | 126 | max = inFloat; |
jstroud | 0:af4f83b43162 | 127 | float screenY = map(inFloat, 1.0, 0, 0, height); |
jstroud | 0:af4f83b43162 | 128 | |
jstroud | 0:af4f83b43162 | 129 | redrawBG(); |
jstroud | 0:af4f83b43162 | 130 | // draw the line from bottom of screen to desired height |
jstroud | 0:af4f83b43162 | 131 | // choose color based on side of threshold we're on |
jstroud | 0:af4f83b43162 | 132 | float texty = map(threshold,0,1,height,0); |
jstroud | 0:af4f83b43162 | 133 | int percent = round(inFloat*100); |
jstroud | 0:af4f83b43162 | 134 | String hitText = ""; |
jstroud | 0:af4f83b43162 | 135 | if(inFloat<threshold) { |
jstroud | 0:af4f83b43162 | 136 | fill(61,126,155); |
jstroud | 0:af4f83b43162 | 137 | hitText = "LAME"; |
jstroud | 0:af4f83b43162 | 138 | } else { |
jstroud | 0:af4f83b43162 | 139 | hitText = percent + "%"; |
jstroud | 0:af4f83b43162 | 140 | fill(231,76,70); |
jstroud | 0:af4f83b43162 | 141 | } |
jstroud | 0:af4f83b43162 | 142 | //println(inFloat); |
jstroud | 0:af4f83b43162 | 143 | textSize(72); |
jstroud | 0:af4f83b43162 | 144 | text(hitText,20,texty-40); |
jstroud | 0:af4f83b43162 | 145 | rect(width/2-20 , height, 40, -1*(height - screenY)); |
jstroud | 0:af4f83b43162 | 146 | } |
jstroud | 0:af4f83b43162 | 147 | } |
jstroud | 0:af4f83b43162 | 148 | |
jstroud | 0:af4f83b43162 | 149 | void redrawBG() { |
jstroud | 0:af4f83b43162 | 150 | background(236,240,241); |
jstroud | 0:af4f83b43162 | 151 | stroke(52,73,94); |
jstroud | 0:af4f83b43162 | 152 | float ty = height-map(threshold,0,1,0,height); |
jstroud | 0:af4f83b43162 | 153 | line(0,ty,width,ty); |
jstroud | 0:af4f83b43162 | 154 | fill(255,0,0); |
jstroud | 0:af4f83b43162 | 155 | stroke(0,0,0); |
jstroud | 0:af4f83b43162 | 156 | textSize(100); |
jstroud | 0:af4f83b43162 | 157 | text("Test your strength!",width/5.7,100); |
jstroud | 0:af4f83b43162 | 158 | } |
jstroud | 0:af4f83b43162 | 159 | */ |
jstroud | 0:af4f83b43162 | 160 |