Processing keeps resetting my servos

edited May 2014 in Arduino

hi, my code is about tracking moving objects and making the servos follow it, the code worked (with some issues that I posted before) but I found out that it was resting the servos position once the object stops moving. I have been up and down on this code to find out why and to stop it or put a delay and I couldn't find out why.

here is the code. (PS: it needs an arduino to open also change the arduino com #) if you have servos I can post the arduino code here if you want

import processing.video.*;
import processing.serial.*;  
Capture video;
int xpos=0; 
int ypos=0;
Serial port; 
PImage prevFrame;

float threshold = 100;
int Mx = 0;
int My = 0;
int ave = 0;

int ballX = width*8;
int ballY = height*8;
int rsp = 5;

void setup() {
  size(640,480);
  video = new Capture(this, width, height, 30);
 video.start();
  prevFrame = createImage(video.width,video.height,255);
  println(Serial.list()); 
  port = new Serial(this, "COM4", 19200);
}

void draw() {


  if (video.available()) {

    prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); 
    prevFrame.updatePixels();
    video.read();
    image(video, 0, 0,360,360);
    update(Mx, My);

  cursor(CROSS);
  }

  loadPixels();
  video.loadPixels();

  Mx = 0;
  My = 0;
  ave = 0;


  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {

      int loc = x + y*video.width; 
      color current = video.pixels[loc];      
      color previous = prevFrame.pixels[loc]; 


      float r1 = red(current); 
      float g1 = green(current);
      float b1 = blue(current);
      float r2 = red(previous);
      float g2 = green(previous); 
      float b2 = blue(previous);
      float diff = dist(r1,g1,b1,r2,g2,b2);


      if (diff > threshold) { 
        pixels[loc] = video.pixels[loc];
        Mx += x;
        My += y;
        ave++;
      } else {

        pixels[loc] = video.pixels[loc];
      }
    }
  }
  rect(0,0, width, height);
  if(ave != 0){ 
    Mx = Mx/ave;
    My = My/ave;
  }
  if (Mx > ballX + rsp && Mx > 50){
    ballX+= rsp;
  }else if (Mx < ballX - rsp && Mx > 50){
    ballX-= rsp;
  }
  if (My > ballY + rsp && My > 50){
    ballY+= rsp;
  }else if (My < ballY - rsp && My > 50){
    ballY-= rsp;
  }

    updatePixels();
    strokeWeight(1.0);
    stroke(0);
    fill(99, 255,99, 90);
    ellipse(ballX, ballY, 15, 15);


}
void update(int Mx, int My)
{
  xpos= Mx/2;
  ypos = My/2;

  port.write(xpos+"Mx");
  port.write(ypos+"My");
}

Answers

  • It is indeed a question about code, but I moved the topic to a more specialized category: always choose the most specialized category, to find the best audience for your topic.

  • thank you

  • If you're using more than one servo, are you using a power supply? Just asking... I had two servos act funny because the Arduino could not power them without a power supply.

    Also, to test, you could replace the servos with some drawings in your screen, to make sure it's the code and not the hardware.

  • The servos (2 servos) are running on an external power supply of 4.5A and 6v (more than it needs)

    the issue is software, the servos track the object perfectly but when the object stops moving, the servos go back to their original position.

    so I need to know where to put a 5 or 10 sec delay. that's all

  • It's hard to suggest anything without seeing what goes wrong. A video of the problem would help. Or a version that does not need Arduino (that simulates the Arduino by drawing on the screen, as I suggested above, so we could run the program and see it fail).

    What is happening in front of the webcam? What should happen during the 5 or 10 delay? Nothing? When should the delay begin?

  • edited May 2014

    here is what happens and what should happen also I will leave the code for tracking that does not need arduino or servos and will also post the arduino code:

    **What Code Does And Has My Problem: ** PS: The problem is at 10 and 11 and I don't know if processing or arduino is the one that is resetting my servos

    1-code runs 2-camera on 3-I walk in front if the camera 4-processing tracks me 5-processing sends the location of me to arduino 6-arduino moves the servos 7-servos track me and follow me 8-I stop walking but still in camera frame 9-processing does not catch a moving object 10-processing send nothing to arduino 11-arduino command the servos to go back to center position

    **What I Want The Code To Do: ** 1-same--code runs 2-same--camera on 3-same--I walk in front if the camera 4-same--processing tracks me 5-same--processing sends the location of me to arduino 6-same--arduino moves the servos 7-same--servos track me and follow me 8-same--I stop walking but still in camera frame 9-same--processing does not catch a moving object 10-processing send nothing to arduino 11-arduino takes 5 to 10 sec delay then reset servo to center position

    This is the processing code for object tracking and you can use it to test it. PS:it still has some problems that I posted about it in here http://forum.processing.org/two/discussion/4389/motion-tracking#Item_12 and herehttp://forum.processing.org/two/discussion/4507/motion-detection#Item_1

    so never mind about those issues, the code is enough to explain the problem

        import processing.video.*;
    
    Capture video;
    
    PImage prevFrame;
    
    float threshold = 150;
    int Mx = 0;
    int My = 0;
    int ave = 0;
    
    int ballX = width/8;
    int ballY = height/8;
    int rsp = 5;
    
    void setup() {
      size(320, 240);
      video = new Capture(this, width, height, 30);
      video.start();
      prevFrame = createImage(video.width, video.height, RGB);
    }
    
    void draw() {
    
    
      if (video.available()) {
    
        prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
        prevFrame.updatePixels();
        video.read();
      }
    
      loadPixels();
      video.loadPixels();
      prevFrame.loadPixels();
    
      Mx = 0;
      My = 0;
      ave = 0;
    
    
      for (int x = 0; x < video.width; x ++ ) {
        for (int y = 0; y < video.height; y ++ ) {
    
          int loc = x + y*video.width;            
          color current = video.pixels[loc];      
          color previous = prevFrame.pixels[loc]; 
    
    
          float r1 = red(current); 
          float g1 = green(current); 
          float b1 = blue(current);
          float r2 = red(previous); 
          float g2 = green(previous); 
          float b2 = blue(previous);
          float diff = dist(r1, g1, b1, r2, g2, b2);
    
    
          if (diff > threshold) { 
            pixels[loc] = video.pixels[loc];
            Mx += x;
            My += y;
            ave++;
          } 
          else {
    
            pixels[loc] = video.pixels[loc];
          }
        }
      }
      fill(255);
      rect(0, 0, width, height);
      if (ave != 0) { 
        Mx = Mx/ave;
        My = My/ave;
      }
      if (Mx > ballX + rsp/2 && Mx > 50) {
        ballX+= rsp;
      }
      else if (Mx < ballX - rsp/2 && Mx > 50) {
        ballX-= rsp;
      }
      if (My > ballY + rsp/2 && My > 50) {
        ballY+= rsp;
      }
      else if (My < ballY - rsp/2 && My > 50) {
        ballY-= rsp;
      }
    
      updatePixels();
      noStroke();
      fill(0, 0, 255);
      ellipse(ballX, ballY, 20, 20);
    }
    

    Here is the arudino code:

    #include <Servo.h> 
    
    Servo yservo;  Servo xservo; 
    
    int ypos = 0; 
    int xpos= 0;
    
    void setup(){
      xservo.attach(14);
      yservo.attach(15); 
    
      Serial.begin(19200); 
      Serial.println("Rolling"); 
    
    }
    
    void loop() {
      static int v = 0; 
      if ( Serial.available()) {
        char ch = Serial.read(); 
        switch(ch) { 
            case '0'...'9': 
            v = v * 10 + ch - '0';
    
            break;
          case 'x': 
    
            xservo.write(v);
            v = 0;
            break;
          case 'y':
            yservo.write(v);
            v = 0;
            break;
        }
      }
    }
    

    Thank You

  • I don't know why the steps are side to side and not one in each line!!!!

Sign In or Register to comment.