Priyam Patel

Lab 9

Net ID: pp386

Objective: Mapping

In this lab, I attempt to create a map of the lab area using the time of flight sensor on the car and rotating the car on its own axis. This would involve either using a PID controller with the gyroscope readings as inputs to get ht accurate angular readings of th car, although after trying and failing to make the car rotate on its own axis through PID, I used an open loop function to make the car rotate.

Prelab

I revised lecture 2 on transformation matrices to do this lab.

Lab Procedure

Rotating the robot on its own axis

I used a PID controller with the gyroscope reading as the input and motor speed as the output of the controller. The controller controls the movement of the car and the setpoint is the degrees per second it moves. I initially used the following controller however, I wasnt able to make the car rotate on its own axis smoothly, a lot of the times the static friction was not overcome and sometimes only one set of wheels overcame the friction causing the car to just go in one direction rather than spin on its own axis.

double Setpoint=50;
double prev_error=0.0;
double cur_error;
double Input, Output;
double Kp=3, Ki=0.2, Kd=0;
double cur_time=0.0;
double prev_time=0.0;
double integrator_sum=0.0;
double current_point;
int flag=0;
double degreess=0.0;
double prev_gyr=0.0;
double prev_t;
double values[100];
int indexx=0;
void PID_Controller(double Setpoint,double current_point)
{ cur_time=millis();
  cur_error= Setpoint-current_point;
  double p=Kp*cur_error;
  integrator_sum+=cur_error;
  double i= Ki*integrator_sum*(cur_time-prev_time);
  double d=Kd*(cur_error-prev_error)/(cur_time-prev_time) ;
  Output = p + i + d;
  Serial.print(Output);
  prev_error=cur_error;
  prev_time=cur_time;
}

Though correct in theory, my PID controller was not successful to make the car go round, and since I was short on time before spring break, I decided to implement open loop control to rotate the car with a constant speed, stop the car to take the distance reading and again rotate the car. I achieved this through the following code snippet.

analogWrite(16, 0); 
  analogWrite(14, 130);
  analogWrite(7, 135);
  analogWrite(3, 0);
  delay(500);
   if (myICM.dataReady())
        {
          myICM.getAGMT();         // The values are only updated when you call 'getAGMT'
             // Uncomment this to see the raw values, taken directly from the agmt structure
          current_point=printScaledAGMT(&myICM);
          //Serial.println(x);
          // This function takes into account the scale settings from when the measurement was made to calculate the values with units
          //delay(30);
        }
        else
        {
          Serial.println("Waiting for data");
          //delay(500);
        }
  delay(500);
  analogWrite(16, 255); 
  analogWrite(14, 255);
  analogWrite(7, 255);
  analogWrite(3, 255);
  delay(500);
  distanceSensor.startRanging();
  //distanceSensor2.startRanging();//Write configuration bytes to initiate measurement
  int distance = distanceSensor.getDistance(); 
  x=distance;
//Get the result of the measurement from the sensor
  distanceSensor.clearInterrupt();
  distanceSensor.stopRanging();
  delay(500);

The below video shows the data collection process over bluetooth. I collected ToF readings from 5 points marked on the lab floor. (5,3),(5,-3),(-3,-2), (0,0) and (0,3)

Mapping the data

For mapping the data, I take all the data into text files and use Python to create the Polar Plots and the final map after the translations. image
image
Polar Plot and scatter plot for (-3,-2) reading.
image Overall reading image

Line Based Map

I saved the values for the detected map.

X6=[-1600,-1600,1950,1950,-750,-750,-1600]
Y6=[250,-1250,-1250,1340,1340,250,250]
X7=[-125,-125,500,500,-125]
Y7=[-500,-1250,-1250,-500,-500]
X8=[800,800,1400,1400,800]
Y8=[400,-100,-100,400,400]

Conclusion

In this lab, I was able to map the lab setup surprisingly well even with an open loop control without PID. This makes PID optional, although theoretically, we can gain better precision with PID and angular data. I thoroughly enjoyed doing this lab as I got to use the robot for some task that it was meant to do and I am glad to see my robot finally working.