Go Down

Topic: upgrading scanning sonar project (Read 2613 times) previous topic - next topic

urthlight

I am trying to upgrade the scanning ultrasonic SONAR project. I want to add some buttons on the Processing Screen that will change Azimuth, Bearing, Range and Tilt for a second servo. I started with the handshaking Demo.I have the SONAR data coming in but am having trouble getting the pan and tilt working. Is anyone else looking to build a robot scanning SONAR unit? I welcome any assistance.


Arduino code
Code: [Select]
char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
boolean ledState = LOW; //to toggle our LED


/////////////////////////////////////////////////////////////////
// sonar init////////////////////////////////////////////////////

#include <NewPing.h>

#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     4  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

/////////////////////////////////////////////////////////////////
//////////////////pan and tilt init/////////////////////////////
#include <Servo.h>

Servo Servo_1;
Servo Servo_2;



/////////////////////////////////////////////////////////////////
void setup()
{
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  //initialize serial communications at a 9600 baud rate
  Serial.begin(115200);
  establishContact();  // send a byte to establish contact until receiver responds

  //////////////////////////////////////////////////////////////////////
  //pan and tilt setup////////////////////////////////////////////////////////
 pinMode(1,OUTPUT);
  Servo_1.attach(5); //analog pin 0


  Servo_2.attach(6); //analog pin

  //////////////////////////////////////////////////////////////////////
}
void loop()
{
 
  if (Serial.available() > 0) { // If data is available to read,
    val = Serial.read(); // read it and store it in val

    if(val == '1') //if we get a 1
    {
        manualservomove();

       ledState = !ledState; //flip the ledState
       digitalWrite(ledPin, ledState);
    }
    delay(100);
  }
    else {
 //GetRange();
  Serial.println(sonar.ping_cm());
  //Serial.println("q"); //send back a hello world
    delay(50);
    }
}
void establishContact() {
  while (Serial.available() <= 0) {
  Serial.println("A");   // send a capital A
  delay(300);
  }
}



////////////////////////////////////////////////////////
void GetRange(){
 delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 // Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  //Serial.println("cm");
}

///////////////////////////////////////////////////////


void manualservomove()
{


  static int v = 0;

  if ( Serial.available()) {
    char val = Serial.read();

    switch(val) {
      case '0'...'9':
        v = v * 10 + val - '0';
        break;
      case 'a':
        Servo_1.write(v);
        Serial.println(v);
        v = 0;
        break;
      case 'b':
        Servo_2.write(v);
        Serial.println(v);
        v = 0;
        break;
     
    }
  }
 }

 ////////////////////////////////////////////////////////
 void buttons(){
  if(Serial.available()){  //id data is available to read

    char val = Serial.read();

    if(val == 'r'){       //if r received
      digitalWrite(13, HIGH); //turn on red led
      }
    if(val == 'l'){       //if b received
      digitalWrite(13, HIGH); //turn on blue led
      }
    if(val == 'y'){       //if y received
      digitalWrite(13, HIGH); //turn on yellow led
      }
    if(val == 'f'){       //if f received
      digitalWrite(11, LOW); //turn off all led
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
      }     
   }
}

///////////////////////////////////////////////////////////
 






Processing code

Code: [Select]
import processing.serial.*; //import the Serial library
Serial myPort;  //the Serial port object
String val;
// since we're doing serial handshaking,
// we need to check if we've heard from the microcontroller
boolean firstContact = false;
/////////////////////////////////////////////////////////////
////pnt//////////
import controlP5.*;
ControlP5 cp5;
int pan = 10;
int tilt = 10;
Serial port;
//ControlP5 cp5; //create ControlP5 object
PFont font;

////////////////////////////////////////////////////////



void setup() {
  size(900, 900);
  noStroke();
  port = new Serial(this, Serial.list()[0], 115200);
  //size(200, 200); //make our canvas 200 x 200 pixels big
  size(900, 900);

  //  initialize your serial port and set the baud rate to 9600
  // port = new Serial(this, Serial.list()[0], 115200);
  port.bufferUntil('\n');
  // myPort = new Serial(this, Serial.list()[0], 115200);
  // myPort.bufferUntil('\n');


  ///////////////////////////////////////////////////////////////////////
  ///////pan n tilt//////

  cp5 = new ControlP5(this);

  // add a horizontal slider for pan
  cp5.addSlider("pan")
    .setPosition(300, 30)
    .setRange(0, 180)
    .setValue(81)
    ;

  // add a horizontal slider for tilt
  cp5.addSlider("tilt")
    .setPosition(300, 60)
    .setRange(0, 180)
    .setValue(146)
    ;

  //////////////////////////////////////////////////////////////////
}






void draw() {

  ////////////////////////////////////////////
  /////pnt/////////////
  // output the servo positions for servos
  // port.write("b"+pan);
  // port.write("a"+tilt);


  ////////////////////////////////////////////
}
void serialEvent( Serial port) {
  //put the incoming data into a String -
  //the '\n' is our end delimiter indicating the end of a complete packet
  val = port.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (val != null) {
    //trim whitespace and formatting characters (like carriage return)
    val = trim(val);
    println(val);

    //look for our 'A' string to start the handshake
    //if it's there, clear the buffer, and send a request for data
    if (firstContact == false) {
      if (val.equals("A")) {
        port.clear();
        firstContact = true;
        port.write("A");
        println("Serial Link Established");
      }
    } else { //if we've already established contact, keep getting and parsing data
      println(val);

      if (mousePressed == true)
      {                           //if we clicked in the window
  /////////////////////////////////////////////////
     
  port.write("b"+pan);//send movement data for servos
  port.write("a"+tilt);
 
     
     ///////////////////////////////////////////////// 

        port.write('1');        //send a 1
        println("1");
      }


      // when you've parsed the data you have, ask for more:
      port.write("A");
    }
  }
}

Idahowalker

I am curious why you are using
Code: [Select]
Servo_1.attach(5); //analog pin 0
  Servo_2.attach(6); //analog pin
why you are not using one of the PWM pins for a servo?

urthlight

I lifted the code from a working snippet.

urthlight









I am using a nano, pins 5 and 6 are pwm pins.

TheMemberFormerlyKnownAsAWOL

I am curious why you are using
Code: [Select]
Servo_1.attach(5); //analog pin 0
  Servo_2.attach(6); //analog pin
why you are not using one of the PWM pins for a servo?
S/he's using the Servo library - any pin will do.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Go Up