Go Down

Topic: robotics car  (Read 1 time) previous topic - next topic

meganator1234

Hello I am a middle school stem club student working on a project and am having problems with this code. We are having problems with libraries and declaring the variable obstacle. Anything would help!!!!




// *****************************************************************************************************************************************************************
// Autonomous_RC: Sketch to control an autonomus race car that will loop in an oval closed race track
// the car will maintain a pre-set distance from the fence of the track using an ultrasonic sensor to range the distance
// Designed and developed by Juan and Juan Loreto on April of 2015
// Version 0.33
//
// Copyright (c) <2015>, <Horsepower & Iditarod>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//*******************************************************************************************************************************************************************



// Libraries to be used in this sketch
#include <Servo.h>
#include <NewPing.h>
#include <PID_v1.h>

// Constants definitions

// PIN assignment for ultrasonic sensor (have to be DIGITAL pins)
#define S1_TRIGGER_PIN 7
#define S1_ECHO_PIN 6
#define S2_TRIGGER_PIN 5
#define S2_ECHO_PIN 4

// PIN assignment for steering servo (has to be PWM pin)
#define STEERING_SERVO_PIN 10

// PIN assignment for ESC (has to be PWM pin)
#define ESC_SERVO_PIN 11

// Tuning values
#define KP 1                          // this is the gain for Proportional correction
#define KI 0.75                       // this is the gain for Integral correction
#define KD 0.50                       // this is the gain for Derivative correction
#define DISTANCE_FROM_OBSTACLE 100    // this is the max distance from a wall in front
#define SONAR_MAX_DISTANCE 200        // this is the maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define STEER_LEFT_VALUE 130          // this is the value to completely turn left
#define STEER_RIGHT_VALUE 50          // this is the value to completely turn right
#define STEER_CENTER 90               // this is the value to completely center the front wheels
#define STEER_RIGHT_END_TURN 70        // this is the turn to right after the turn
#define MAX_SPEED 179                 // this is the value of the highest speed
#define STOP 90                       // this is the value to stop the motor
#define TURN_SPEED 97
#define CRUISE_SPEED 100              // this is the value of the cruise speed
#define GET_READY 5000                // this is the time we want the car to wait while we set it up after power it on (milliseconds)
#define RACE_TIME 60000               // this is the time the car will be running in the track (milliseconds)


// Variables definition

double Setpoint, Input, Output, obstacle; // This variables are required by the PID library, the 3 k variables are initialized with the tuning values;
unsigned long iinitialTime, currentTime;
boolean run, debug, turn;


// Objects definition
Servo steering, esc; // This line creates two objects of the type Servo, one for the steering servo and one for the ESC.
NewPing sonarS(S1_TRIGGER_PIN, S1_ECHO_PIN, SONAR_MAX_DISTANCE); // NewPing setup of pins and maximum distance for side ultrasonic sensor
NewPing sonarF(S2_TRIGGER_PIN, S2_ECHO_PIN, SONAR_MAX_DISTANCE); // NewPing setup of pins and maximum distance for front ultrasonic sensor


void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to send information.
 
  // Links the Servo objects with the PINS on the Arduino board
  steering.attach(STEERING_SERVO_PIN);
  esc.attach(ESC_SERVO_PIN);
 

 
 
  // Initializes the positions of the servo and ESC
  esc.write(STOP);  // Stops the car
  steering.write(STEER_CENTER); // Centers the front wheels
 
  // wait for the car to be setup on the track
  delay(GET_READY); // This delay allows you to get the car set in the track once it has been powered on
 
  run = true; 
  debug = false;
  turn = false;
}

void loop() {
  while(run){
    esc.write(CRUISE_SPEED); //this line makes the car to accelerate up to the cruise speed
//     The next loop is to keep the car running for the time of the race
    while(millis() < (RACE_TIME + GET_READY)) {  // performs this loop until the race time is reached.
//obstacle = sonarF.ping_cm();
//                        if(obstacle <= DISTANCE_FROM_OBSTACLE && obstacle > 0) turn = true;
//      while(obstacle <= DISTANCE_FROM_OBSTACLE && obstacle > 0){     //this loop steers the car to the left if an obstable is in front
                           esc.write(TURN_SPEED);
         steering.write(STEER_LEFT_VALUE);
         delay(29);
   //      obstacle = sonarF.ping_cm();
      }
                        if(turn){
                           esc.write(CRUISE_SPEED);
                           steering.write(STEER_RIGHT_END_TURN);
                           turn = false;
                        }

      do{
        delay(29);
//        Input = sonarS.ping_cm();
//        if (Input == 0 && debug) Serial.println(" Error reading distance, no correction");
//      } while (Input == 0);

//        steering.write(STEER_CENTER - Output);
        if(debug){
          Serial.print("Distance = ");
//          Serial.print(Input);
          Serial.print("  correction = ");
         Serial.println(Output);
        }
          if(debug){
            Serial.print("Distance = ");
//           Serial.print(Input);
            Serial.println(" NO correction performed");
            }       
      }
    }
    esc.write(STOP);
    run = false;
}


jremington

Please read and follow the directions in the "How to use this forum" post.

Be sure to edit your post to add code tags.

PaulS

Code: [Select]
    do{
What is this supposed to do? There is a  do/while statement in C. There is NO do statement.
The art of getting good answers lies in asking good questions.

ardy_guy

Code: [Select]
    do{
What is this supposed to do? There is a  do/while statement in C. There is NO do statement.
I see its corresponding "while" is remmed out.

We are having problems with libraries and declaring the variable obstacle.
Help others help you by explaining exactly what those problems are, and what error messages (if any) you get.


Side note: I wonder what made the authors of the code think that servos managed through the Servo library need to be on pwm pins?


Go Up