Saturday, February 6, 2010

ScanTer Experiments 3

 In this last piece of code (which went through a lot of debugging) we finally managed to crack a lot of problems we had in the past examples. Basically all you need to do is run the code in processing after uploading the firmata code (from the arduino library) to the arduino board. 

Next step is to draw or write something -> the result, after pressing ENTER, is the initial input.

Here is the fully commented script:

  Dessau Institute of Architecture - WS09

The ScanTer
 This script is ment to control/link processing with the arduino board in order to control the motion of a RCTank
 The user gets to choose between a square and a circle. once he made his choice, the tank starts performing that particular shape.

 Created 27 January 2010
 By Pula_Team; Tudor Cosmatu & Grygorii Zotov

 based, more or less, on the arduino "Blink" example

//import the serial function
import processing.serial.*;

//import the arduino library
import cc.arduino.*;

Arduino arduino;

//declare the pin number
int LFPin = 5;
int RFPin = 4;

//declare the 2D array which stores the data from the IRpen movement
float[][] Small = new float[1000][5];

//previous previous x and y pos
float ppx = width/2;
float ppy = height/2;

//declare counter
int counter = 0;

void setup(){
  //set FrameRate -> changing this will result in a malfunction of the tanks movement
  //set the arduino
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  //setting up the pins as outputs
  arduino.pinMode(LFPin, Arduino.OUTPUT);
  arduino.pinMode(RFPin, Arduino.OUTPUT);


void draw(){
  //if mouse is pressed then do the rest
  if (mousePressed ==true){
    //this allows a maximum of 1000 control points
    if (counter < 999){
      // declare the x and y values - IRpen
      float x = mouseX;
      float y = mouseY;
      // declare the previous x and y positions of the IRpen
      float px = pmouseX;
      float py = pmouseY;
      //if px and py are not equal to x and y, then do the rest (if px=x and py=y the resulting movement has to suffer from it since when keeping the IRpen in the same position for too long the resulting vectors are null)
      // generally skipping the null vectors
      if (px!=x || py!=y){
        stroke (255);
        line(px, py, x, y);
        //setting boolean values for motion
        int lfPinA = 1;
        int rfPinA = 1;
        //setting the initial delay to 0
        float del = 0;
        //the distance between two points helps setting the delay for straight movement (forward)
        float distance = dist (px, py, x, y);
        // for this specific RCtank the value needs to be increased (therefore whenever replacing the RCtank a new calibration process needs to happen)
        del = distance*8;
        //storing data to the list
        Small[counter][0]= lfPinA;
        Small[counter][1]= rfPinA;
        Small[counter][2]= del;
        Small[counter][3]= x;
        Small[counter][4]= y;

        //creating 3 vectors for calculating the rotation
        PVector v0, v1, v2;
        //0 vector -> all angle calculations are made according to this vector
        v0 = new PVector(0, 1);
        v2 = new PVector(x-px, y-py);
        v1 = new PVector(px-ppx, py-ppy);
        //calculating the angle between the vectors in order to set the correct delay for the motors (1st and then 2nd one)
        float L1 = PVector.angleBetween(v0, v1);
        L1 = degrees(L1);

        float L2 = PVector.angleBetween(v0, v2);
        L2 = degrees(L2);
        //some math in order to set the correct turning direction
        if ((px-ppx)<0){
          L1 = -L1;

        if ((x-px)<0){
          L2 = -L2;
        // sum of the angles
        float L = 360-L1+L2;
        if (L>360){
        // check angles
        //println ("WWW");
        //println (L);
        //left and right turns
        if (L < 180){
          L = L;
          lfPinA = 0;
          rfPinA = 1;
          del = (1500/180)*L;

        if (L>180){
          lfPinA = 1;
          rfPinA = 0;
          del = (1500/180)*L;

        //print delay value
        //println (del);
        //store the above in the list
        Small[counter+1][0]= lfPinA;
        Small[counter+1][1]= rfPinA;
        Small[counter+1][2]= del;

        counter = counter+2;
        //set the previous x and y pos to previous previous x and y pos and start the loop over
        ppx = px;     
        ppy = py;


//set the counter to skip first 2 steps
int a=2;

void keyPressed(){
      //if enter is pressed he is continuing with the next loop (if not, the tank performs a circular motion in the control last point)
     if (keyCode == ENTER) {
       while (Small[a][0]>0 || Small[a][1]>0 || Small[a][2]>0){

    //create visible dots for previewing the steps

    // Both motors ON
    if (Small[a][0] == 1 && Small[a][1] == 1){
      arduino.digitalWrite(LFPin, Arduino.HIGH);    // set the motor on
      arduino.digitalWrite(RFPin, Arduino.HIGH);   // set the motor on

    // Left motor OFF
    if (Small[a][0] == 0 && Small[a][1] == 1){
      arduino.digitalWrite(LFPin, Arduino.LOW);    // set the motor off
      arduino.digitalWrite(RFPin, Arduino.HIGH);   // set the motor on

    // Right motor OFF
    if (Small[a][0] == 1 && Small[a][1] == 0){
      arduino.digitalWrite(LFPin, Arduino.HIGH);   // set the motor on
      arduino.digitalWrite(RFPin, Arduino.LOW);   // set the motor off

    // Both motors OFF
    if (Small[a][0] == 0 && Small[a][1] == 0){
      arduino.digitalWrite(LFPin, Arduino.LOW);  // set the motor off
      arduino.digitalWrite(RFPin, Arduino.LOW);   // set the motor off
   //after the Enter loop finishes stop both motors
   arduino.digitalWrite(LFPin, Arduino.LOW);
   arduino.digitalWrite(RFPin, Arduino.LOW);

Videos and images will follow...

No comments:

Post a Comment