Basic output of force sensor data to serial, in form "value1/n"

Dependencies:   mbed BLE_API nRF51822

Committer:
jstroud
Date:
Tue Nov 25 01:35:56 2014 +0000
Revision:
4:c28c34fcb120
Parent:
2:697726f1d35c
force sensor testing, all six sensors

Who changed what in which revision?

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