Clearing Serial backlog?
in
Contributed Library Questions
•
1 year ago
Hi all,
I am getting data in from a Kinect and am using it to move the head of an XY plotter back and forth, up and down, etc.
The way I am doing it, is I am assigning certain characters to certain directions and having Processing send them to the Arduino as commands.
The problem is, if I send it a bunch of data, and then the situation changes, it continues to execute the whole sequence of data input from before. For example, if it's mirroring me, and I stand to the left, it sends the plotter head to the left. But if I move right, it keeps sending commands to go left for a time, THEN sends it right.
Is there a way to clear this backlog?
Thanks in advance!!!!!!!!!!
Processing Code:
- //---------------------------------------------
- //Thanks to thomas diewald for dLibs processing library for Kinect
- //Antonio Molinaro for blobscanner library
- import java.util.Arrays; // This may not be needed
- import Blobscanner.*;
- //freenect stuff
- import dLibs.freenect.toolbox.*;
- import dLibs.freenect.constants.*;
- import dLibs.freenect.interfaces.*;
- import dLibs.freenect.*;
- //SERIAL
- import processing.serial.*;
- Serial myPort;
- char keyIndex;
- //-----------------------KINECT STUFF-------------------------------------
- Kinect kinect_;
- KinectFrameVideo kinect_video_;
- KinectFrameDepth kinect_depth_;
- int kinectFrame_size_x = VIDEO_FORMAT._RGB_.getWidth();
- int kinectFrame_size_y = VIDEO_FORMAT._RGB_.getHeight();
- PImage video_frame_, depth_frame_; // images
- // raw depth values of the kinect depth camera ( copy by reference or deep copy)
- int[] raw_depth;
- int tooFar, tooClose;
- float personX, personY;
- boolean reaching = false;
- boolean isPresent;
- int presCounter = 0;
- int absenceCounter = 0;
- //mood variables
- int mood;
- //----------------------------------------------------------------------------
- //-----------------------BLOB SCANNER STUFF-----------------------------------
- Detector bd;
- PImage img;
- PFont f = createFont("", 10);
- //-----------------------------------------------------------------------------
- void setup() {
- println(Serial.list());
- String portName = Serial.list()[0];
- println(portName);
- myPort = new Serial(this, portName, 9600);
- size(kinectFrame_size_x, kinectFrame_size_y*2);
- kinect_ = new Kinect(0);
- kinect_video_ = new KinectFrameVideo(VIDEO_FORMAT._RGB_ );
- kinect_depth_ = new KinectFrameDepth(DEPTH_FORMAT._11BIT_);
- kinect_depth_.setColorMode(2); // no generation of depth-map by default
- kinect_video_.setFrameRate(30);
- kinect_depth_.setFrameRate(30);
- kinect_video_.connect(kinect_);
- kinect_depth_.connect(kinect_);
- // create a PImage for video/depth
- video_frame_ = createImage(VIDEO_FORMAT._RGB_ .getWidth(), VIDEO_FORMAT._RGB_ .getHeight(), RGB);
- depth_frame_ = createImage(DEPTH_FORMAT._11BIT_.getWidth(), DEPTH_FORMAT._11BIT_.getHeight(), RGB);
- //blob detection
- bd = new Detector( this, 0, 0, kinectFrame_size_x, kinectFrame_size_y, 255 );
- mood = 0;
- isPresent = false;
- }
- //-------------------------------------------------------------------
- void draw() {
- background(0);
- // println(frameRate);
- //---------------------------------------
- // DEPTH VALUES
- //---------------------------------------
- raw_depth = kinect_depth_.getRawDepth(); // just copy by reference
- // custom mapping from raw-depth values, to a gray depthmap
- depth_frame_.loadPixels();
- for (int i = 0; i < raw_depth.length; i++) {
- // 2047 is the depth-value-"code" for kinect-shadows
- /*
- } else
- { */ // valid depth values
- // raw depth values have a range of ~330 to ~1150 and are mapped to a gray-values from 255 to 0
- //int gray = ((int) map(raw_depth[i], 330, 1150, 255, 0)) & 0xFF ;
- if (raw_depth[i] > 350 && raw_depth[i] < 720 ) {
- depth_frame_.pixels[i] = 0xFF0000;
- }
- else {
- depth_frame_.pixels[i] = 0x000000;
- }
- }
- //FLAG if anyone reaches ahead
- depth_frame_.updatePixels();
- //---------------------------------------
- // VIDEO VALUES
- //---------------------------------------
- // copy rgb-video-pixels to image (by reference!!)
- video_frame_.loadPixels();
- video_frame_.pixels = kinect_video_.getPixels();
- video_frame_.updatePixels();
- //set up blobtracker thresholding of image
- bd.imageFindBlobs(depth_frame_);
- bd.loadBlobsFeatures();
- image(depth_frame_, 0, 0);
- //image(depth_frame_, 0, DEPTH_FORMAT._11BIT_.getHeight());
- //Draws the bounding box for all the blobs in the image.
- int id = getBiggestBlobIndex(bd);
- bd.findCentroids(false, false);
- if (id > -1) {
- personX = bd.getCentroidX(id);
- personY = bd.getCentroidY(id);
- presCounter++;
- mood++;
- absenceCounter=0;
- isPresent = true;
- }
- if (id < 0){
- absenceCounter++;
- presCounter = 0;
- isPresent = false;
- mood = 0;
- }
- if(isPresent && mood <= 200){
- shyPresent();
- }
- else if(isPresent && mood > 200){
- cheekyPresent();
- }
- //show us where the centroid is
- ellipse(personX, personY, 20, 20);
- }
- void shyPresent(){
- println("Someone is present, but I am shy!");
- if (personX>=300) {
- myPort.write('J'); // Send out serial msg
- }
- if (personX<300) {
- myPort.write('X'); // Send out serial msg
- }
- }
- void cheekyPresent(){
- println("Someone is present, and I am excited!");
- if (personX>=300) {
- myPort.write('Z'); // Send out serial msg
- }
- if (personX<300) {
- myPort.write('H'); // Send out serial msg
- }
- }
- void absent(){
- }
- //thanks to Quarks http://www.lagers.org.uk for this bit of code
- public int getBiggestBlobIndex(Detector d) {
- int nbrBlobs = d.getBlobsNumber();
- // If we have no blobs return
- if (nbrBlobs ==0)
- return -1;
- d.weightBlobs(false);
- BlobWeight[] blobs = new BlobWeight[nbrBlobs];
- for (int i = 0; i < blobs.length; i++)
- blobs[i] = new BlobWeight(i, d.getBlobWeight(i));
- Arrays.sort(blobs);
- return blobs[0].id;
- }
- public class BlobWeight implements Comparable<BlobWeight> {
- public int id;
- public Integer weight;
- public BlobWeight(int id, Integer weight) {
- this.id = id;
- this.weight = weight;
- }
- public int compareTo(BlobWeight b) {
- // Reverse so heaviest are first in list
- return new Integer(b.weight).compareTo(weight);
- }
- }
- void dispose() {
- Kinect.shutDown();
- super.dispose();
- }
Arduino Code:
- /* Stepper Motor Controller ; language: Wiring/Arduino
- This program drives a unipolar or bipolar stepper motor. by Tom Igoe
- and the Spline library by Kerinin
- */
- /* significant help from arduino forum superstars: sirbow2, stimmer
- */
- #include <AFMotor.h>
- #include <Servo.h>
- AF_Stepper xAxis(200, 1);
- AF_Stepper yAxis(200, 2);
- int reading = 0;
- int previous = LOW;
- long time = 0; // the last time the output pin was toggled
- long debounce = 50; // the debounce time, increase if the output flickers
- int state = HIGH;
- //set pin numbers
- int xStopMin = A0;
- int xStopMax = A1;
- int yStopMin = A2;
- int yStopMax = A3;
- int xPos;
- int yPos;
- boolean xIs0;
- boolean xIsMax;
- boolean yIs0;
- boolean yIsMax;
- boolean interruption;
- void setup() {
- xAxis.setSpeed(60);
- yAxis.setSpeed(60);
- //set limit switches to analog inputs
- pinMode(xStopMin, INPUT);
- pinMode(xStopMax, INPUT);
- pinMode(yStopMin, INPUT);
- pinMode(yStopMax, INPUT);
- interruption = false;
- xPos = 0;
- yPos = 0;
- // Initialize the Serial port:
- Serial.begin(9600);
- }
- void loop() {
- //check interrupt
- if(interruption == true){
- int counter;
- counter ++;
- Serial.println("pause for a sec");
- delay(1000);
- Serial.println("ok, do new stuff now");
- interruption = false;
- }
- //check limit switches
- if(digitalRead(xStopMin) == HIGH){
- xIs0 = true;
- xPos = 0;
- Serial.println("xMin");
- }
- else xIs0 = false;
- if(digitalRead(xStopMax) == HIGH){
- xIsMax = true;
- Serial.println("xMax");
- }
- else xIsMax = false;
- if(digitalRead(yStopMin) == HIGH){
- yIs0 = true;
- Serial.println("yMin");
- yPos = 0;
- }
- else yIs0 = false;
- if(digitalRead(yStopMax) == HIGH){
- yIsMax = true;
- Serial.println("yMax");
- }
- else yIsMax = false;
- char val=0;
- if(Serial.available()) val = Serial.read();
- //keyboard control
- switch(val){
- case 'A':
- Serial.read();
- interruption = true;
- break;
- case 'N':
- Serial.read();
- moveN(200);
- break;
- case 'S':
- Serial.read();
- moveS(200);
- break;
- case 'E':
- Serial.read();
- moveE(200);
- break;
- case 'W':
- Serial.read();
- moveW(200);
- break;
- //diagonal (moving motors almost at the same time)
- case 'J':
- Serial.read();
- moveNE(400);
- Serial.println("I am moving North East");
- break;
- case 'H': //NW
- Serial.read();
- moveNW(400);
- break;
- case 'X': //SE
- Serial.read();
- moveSE(400);
- break;
- case 'Z': //SW
- Serial.read();
- moveSW(400);
- break;
- case 'C': //
- Serial.read();
- moveCirc();
- break;
- case 'B': //
- Serial.read();
- moveTo (1100,3150);
- break;
- //resets
- case 'Q': //reset to 0,0
- Serial.read();
- resetFully();
- break;
- case 'P':
- Serial.read();
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.println("y position: ");
- Serial.println(yPos);
- break;
- default:
- Serial.read();
- delay(10);
- break;
- }
- }
- //movement functions
- //first, str aight up down left right
- void moveE(int numSteps){
- int s = 0;
- while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
- {
- Serial.read();
- if(!interruption){
- xAxis.step(1,FORWARD,DOUBLE);
- s++;
- }
- }
- //delay(100);
- xPos += numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- void moveW(int numSteps){
- int s = 0;
- while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
- {
- Serial.read();
- if(!interruption){
- xAxis.step(1,BACKWARD,DOUBLE);
- s++;
- }
- }
- //delay(100);
- xPos -= numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- void moveN(int numSteps){
- int s = 0;
- while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
- {
- Serial.read();
- if(!interruption){
- yAxis.step(1,BACKWARD,DOUBLE);
- s++;
- }
- }
- //delay(100);
- yPos -= numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- void moveS(int numSteps){
- int s = 0;
- while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
- {
- Serial.read();
- if(!interruption){
- yAxis.step(1,FORWARD,DOUBLE);
- s++;
- }
- }
- //delay(100);
- yPos += numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- //diagonals
- void moveNE(int numSteps){
- int s=0;
- if (!digitalRead(xStopMax) && !digitalRead(yStopMin) && s<numSteps){
- for(int s=0; s<numSteps; s++)
- {
- if(!interruption){
- xAxis.step(1,FORWARD,DOUBLE);
- yAxis.step(1,BACKWARD,DOUBLE);
- }
- }
- xPos += numSteps;
- yPos -= numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- }
- void moveSE(int numSteps){
- int s=0;
- if(!digitalRead(xStopMax) && !digitalRead(yStopMax) && s<numSteps){
- for(int s=0; s<numSteps; s++)
- {
- if(!interruption){
- xAxis.step(1,FORWARD,DOUBLE);
- yAxis.step(1,FORWARD,DOUBLE);
- }
- }
- xPos += numSteps;
- yPos += numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- }
- void moveSW(int numSteps){
- int s=0;
- if(!digitalRead(xStopMin) && !digitalRead(yStopMax) && s<numSteps){
- for(int s=0; s<numSteps; s++)
- {
- if(!interruption){
- xAxis.step(1,BACKWARD,DOUBLE);
- yAxis.step(1,FORWARD,DOUBLE);
- }
- }
- xPos -= numSteps;
- yPos += numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- }
- void moveNW(int numSteps){
- int s=0;
- if(!digitalRead(xStopMin) && !digitalRead(yStopMin) && s<numSteps){
- for(int s=0; s<numSteps; s++)
- {
- if(!interruption){
- xAxis.step(1,BACKWARD,DOUBLE);
- yAxis.step(1,BACKWARD,DOUBLE);
- }
- }
- xPos -= numSteps;
- yPos -= numSteps;
- //check where we are
- Serial.print("x position: ");
- Serial.println(xPos);
- Serial.print("y position: ");
- Serial.println(yPos);
- }
- }
- void resetFully(){
- bool bMoveX = true;
- bool bMoveY = true;
- bool doReset = true;
- int count = 0;
- while (doReset == true){
- if (digitalRead(xStopMin)) bMoveX = false;
- if (digitalRead(yStopMin)) bMoveY = false;
- count ++;
- if (bMoveX == true) xAxis.step(1,BACKWARD,DOUBLE);
- if (bMoveY == true) yAxis.step(1,BACKWARD,DOUBLE);
- if (bMoveX == false && bMoveY == false){
- doReset = false;
- }
- if (count == 8000){
- doReset = false;
- }
- }
- Serial.println("done!");
- xPos = 0;
- yPos = 0;
- }
- void moveTo(int newX, int newY){
- int xSteps = newX - xPos;
- int ySteps = newY - yPos;
- int stepDirectionX = (newX > xPos) ? FORWARD : BACKWARD;
- int stepDirectionY = (newY > yPos) ? FORWARD : BACKWARD;
- int nStepsX = abs(xSteps);
- int nStepsY = abs(ySteps);
- int maxSteps = abs(xSteps) > abs(ySteps) ? abs(xSteps) : abs(ySteps);
- for (int i = 0; i < maxSteps; i++){
- if (i < nStepsX){
- bool bMoveX = true;
- if (digitalRead(xStopMin)) bMoveX = false;
- if (digitalRead(xStopMax)) bMoveX = false;
- if (bMoveX == true){
- if(!interruption){
- xAxis.step(1, stepDirectionX, DOUBLE);
- xPos += stepDirectionX;
- }
- }
- }
- if (i < nStepsY){
- bool bMoveY = true;
- if (digitalRead(yStopMin)) bMoveY = false;
- if (digitalRead(yStopMax)) bMoveY = false;
- if (bMoveY == true){
- if(!interruption){
- yAxis.step(1, stepDirectionY, DOUBLE);
- yPos += stepDirectionY;
- }
- }
- }
- }
- }
- void moveCirc(){
- if(!interruption){
- moveNE(200);
- }
- if(!interruption){
- moveE(100);
- }
- if(!interruption){
- moveSE(200);
- }
- if(!interruption){
- moveS(200);
- }
- if(!interruption){
- moveSW(200);
- }
- if(!interruption){
- moveW(100);
- }
- if(!interruption){
- moveNW(200);
- }
- }
1