Please Help.. Basic template of RADAR..

edited August 2015 in Arduino

Hello guys.. i am new to processing IDE. I have a code of a basic Radar spotting template. when using Arduino IDE i can see the values coming back from sensor using serial print. But in processing this code is not drawing the objects detected in a range. Please help what changes i have to make. in this code.. i got this code from website..

import processing.serial.*;
Serial port;
Serial port2;
String data = "";
String Radius = "";
String Theta = "";
int index = 0;
float distance = 0;
float angle = 0;
float pi = 22.0/7;

void setup() 
{
 
 size(1000,1000);
 background(255,255,255);
  ellipse(500,500,1000,1000);
 line(500,0,500,1000);
 line(0,500,1000,500);
 line(500,500,1000,0);
 line(500,500,0,0);

port = new Serial(this, "COM3", 9600);
port.bufferUntil('.');
}

void draw()

{


}

void serialEvent(Serial port)
{

  data = port.readStringUntil('.');
  data = data.substring(0, data.length() - 1);  
  index = data.indexOf(",");
  Radius = data.substring(0, index);
  Theta = data.substring (index+1 , data.length());
  
   translate(500,500);
   point (0,0);
   
      distance = float(Radius); 
      angle = float(Theta) /180 * pi; 
      fill(30,200,30);
      ellipse(distance * cos(angle) ,  -1 * distance * sin(angle) , 5,5);


}

Any suggestions is worth a gold... Thank you

Answers

  • See How to format code and text, particularly about the order of operations...

  • i corrected it.. please help me

  • anyone please help people

  • Answer ✓

    I know nothing about serial port and Arduino, not having one. BTW, this should be in the Arduino category. I will move it.

    But are you use the COM10 port is the right one?

  • Its actually processing code.. but only link is that it reads the values from arduino UNO board serially

  • Answer ✓

    can you post the arduino code?

    ( Arduino category just means it's Arduino related)

  • There is a PI constant you can use instead of defining it yourself.

    In fact there's a degree () / radians () method you can use for converting between the two (line 47)

    Try printing out Radius and Theta and paste the values here.

  • @Chrisir Here is the Arduino code

    #define ECHOPIN 7        // Pin to receive echo pulse 
    #define TRIGPIN 8
    #include 
    
    Servo myservo; // create servo object to control a servo
    int pos = 0; // variable to store the servo position
    void setup() {
     Serial.begin(9600); 
    
      myservo.attach(9); // attaches the servo on pin 9 to the servo object
      pinMode(3,OUTPUT);
      pinMode(12,OUTPUT);
      pinMode(13,INPUT);
      pinMode(ECHOPIN, INPUT); 
      pinMode(TRIGPIN, OUTPUT); 
    }
    
    void Print (int R , int T)
    {
       Serial.print(R);Serial.print(", ");
       Serial.print(T);Serial.println(".");
       delay(100);
    }
    
    float Distance () {
      
      digitalWrite(TRIGPIN, LOW); 
      delayMicroseconds(2); 
      digitalWrite(TRIGPIN, HIGH); 
      delayMicroseconds(10); 
      digitalWrite(TRIGPIN, LOW);
      // Distance Calculation
      
      float distance = pulseIn(ECHOPIN, HIGH); 
      distance= distance/58; 
      return(distance);
    }
    
    
  • edited August 2015

    @koogs here is the angle and Radius values

    CONSLOE OUT
    
    Angle 0.78571427
    Radius 170
    Angle 0.83809525
    Radius 
    170
    Angle 0.89047617
    Radius 
    170
    Angle 0.94285715
    Radius 
    170
    Angle 0.99523807
    Radius 
    170
    Angle 1.0476191
    Radius 
    156
    Angle 1.0999999
    Radius 
    155
    Angle 1.152381
    Radius 
    17
    Angle 1.2047619
    Radius 
    5
    Angle 1.2571429
    Radius 
    15
    
  • Answer ✓

    after 37 do a println(data);

  • Answer ✓

    do println distance * cos(angle

    also try trim on data after line 37

    do you receive anything at all?

  • Answer ✓

    are those newlines in the data or just part of your formatting? because they could cause parse errors.

    i always print delimiters around things so that i can see - println("Data [" + data + "]");

    try, as chrisir points out, printing the derived values - angle and distance. we only need a dozen, not 200 8)

  • guys its actually printing in the graph but its printing in the top left corner of the window the image is shown below Untitled

  • thanks for the support guys i understood how to fix it.. here i am submitting a correct code here

    import processing.serial.*;
    Serial port;
    Serial port2;
    String data = "";
    String Radius = "";
    String Theta = "";
    int index = 0;
    float distance = 0;
    float angle = 0;
    float pi = 22.0/7;
    float x;
    float y;
    
    void setup() 
    {
     
     size(1000,1000);
     background(255,255,255);
      ellipse(500,500,1000,1000);
     line(500,0,500,1000);
     line(0,500,1000,500);
     line(500,500,1000,0);
     line(500,500,0,0);
    
    port = new Serial(this, "COM3", 9600);
    port.bufferUntil('.');
    }
    
    void draw()
    
    {
       translate(500,500);
       point (0,0);
    ellipse(x , -y, 20, 20);
          fill(7,7,234);
    
    }
    
    void serialEvent(Serial port)
    {
    
      data = port.readStringUntil('.');
      data = data.substring(0, data.length() - 1);  
      index = data.indexOf(",");
      Radius = data.substring(0, index);
      Theta = data.substring (index+1 , data.length());
      
       //translate(500,5000);
      // point (0,0);
       
          distance = float(Radius); 
          angle = float(Theta) /180 * pi; 
          x=distance * cos(angle);
          y=distance*sin(angle);
           print("\nAngle",x);
          print("\nRadius",y);
        //  ellipse(x , y, 20.0, 20.0);
         // fill(30,200,30);
          
    
    }
    
  • Answer ✓

    so the issue was to have it in draw() is better than to have it in serialEvent() ?

  • Answer ✓

    another thing - the translate() in serialEvent() would be cumulative

  • Answer ✓

    ah, good one!

  • Ya@ Chirsir

    And @ koogs cumulative you mean? I am quite confused now.

  • Answer ✓

    basically in the old version the translate do add up, because they never get resetted - so it would be soon outside the window

    nvm

    in the new version it takes place in draw which runs on and on but resets the matrix every new loop

  • oh okay.. got it..

Sign In or Register to comment.