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

Dependencies:   mbed BLE_API nRF51822

Committer:
jstroud
Date:
Tue Nov 11 01:09:06 2014 +0000
Revision:
1:d80d9ec38f57
Parent:
0:44196fc76b4f
Child:
2:697726f1d35c
force sensor test code

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