PIC32MX: Interfacing with Force Sensors from a Scale

From Mech
Revision as of 23:41, 8 February 2010 by JoshuaPeng (talk | contribs) (→‎Code)
Jump to navigationJump to search

Original Assignment

Do not erase this section!

Your assignment is to use four analog inputs to continuously read four force sensors taken from a bathroom scale, and to display them on a PC screen as bars whose length corresponds to the force at the sensors. You will use Processing for the PC data acquisition and graphics.

Overview

Summarize briefly what the page is about.

Circuit

Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.

Code

Where possible, make it a single piece of well-commented cut-and-pastable code, or at least make each function that way, so others can easily copy it. Most comments should be in the code itself; outside the code (on the wiki) should only be explanatory comments that are too cumbersome to include in the code.

The Processing portion of our lab involves 4 sections:

1. colorbars:

continuously displaying the force applied onto each force sensor by using 4 bars that will grow or shrink depending on the amplitude of mass (force). The bars will also show the amplitude of the mass by changing colors. When 0 or little force is applied, then corresponding bar is black colored, at the max force, the bar is orchid (pink) colored.

2. xy-plot:

continuously displays the x- and y- position of the center of balance calculated between all 4 sensors with a dot on a xy-coordinate plane. So, if the person shifts his weight to the right side of scale, then the dot will move to the right in the x direction. If the person shifts his weight to the front of the sensor, then the dot will move up in the y direction.

3. x-position vs. time:

continuously plots a dot for the x-position along a time axis. This plot will be useful to see how much the person shifts his/her weight to the left or right (sagittal plane) over time. Due to the window size and the speed of processing, the points will be plotted over a time period of about 20 seconds.

4. y-position vs. time:

continuously plots a dot for the y-position along a time axis. This plot will be useful to see how much the person shifts his/her weight forward or backwards (coronal or frontal plane) over time. The code for this section is essentially the same as the x-position but edited to show y-position.

General Notes:

Our code was written with the assumption that the inputs from the PIC (that initially comes from the 4 force sensors) will come in the form of a 4x1 array. It does not actually arrive in that form from the PIC, it actually arrives as a string of floats separated by spaces ending with a new line. The new line is deleted and the floats are extracted out, normalized between 0-255, and put into the "mass" array. All of the normalization, window dimension parameters, rectangle dimensions, etc. were set up with the 0-255 range and 1000x700 window size in mind. So if the values are not between 0-255 or the window size is altered, almost all of following code will be messed up. The x-position vs. time and y-position vs. time plots are not implemented as independent functions. The code for these plots are found at the very end of colorbars();.

Global Variables

 //initialization of the serial variable that will hold the values sent from the PIC via RS232
 import processing.serial.*; 
 Serial[] myPorts = new Serial[1];

 //the PIC will output 4 floats and we will first store the values into these 4 variables
 float input0;
 float input1;
 float input2;
 float input3;

 //dimensions of our window will be 1000x700
 float window_height = 700;
 float window_width = 1000;
 PFont fontA;  //initializing our text font

 //the main "mass" 4x1 array that holds the values continuously sent from the 4 force sensors
 //we will refer to this array as the "mass array" in our comments
 float[] mass = new float[4]; 

 //window dimension variable that is used to make the bargraph.  The bx and by variables are
 //used so that the bargraph section could be more easily formatted in case we change the
 //window size.
 float bx = window_width/2/8;
 float by = window_height/2/8;
 int index;  

 //color variables used to indicate the amplitude of the force applied to each sensor
 color c0;
 color c1;
 color c2;
 color c3;

Main Setup Function

void setup() {
  
  index = 0;
  InitSerial();
  smooth();

  //splitting the window into 5 separate areas and filling them with a 102 gray color
  size(int(window_width),int(window_height));
  fill(102);
  rect(0,0,window_width/2,window_height/3);
  fill(102);
  rect(0,window_height/3,window_width/2,window_height/3);
  fill(102);
  rect(0,window_height*2/3,window_width/2,window_height/3);
  fill(102);
  rect(window_width/2,0,window_width/2,window_height/2);
  fill(102);
  rect(window_width/2,window_height/2,window_width/2,window_height/2);

  //filling inside of the x-position vs. time and y-position vs. time plots with white color
  fill(255);
  rect(25, int(window_height/3)+29, int(window_width/2)-49, int(window_height/3)-59);
  fill(255);
  rect(25, int(window_height*2/3)+29, int(window_width/2)-49, int(window_height/3)-59);
}