Aleira N. Sanchez
  • Labs
    Lab 1: Artemis and Bluetooth Lab 2: IMU Lab 3: ToF Lab 4: Motor Drivers and Open Loop Control Lab 5: Linear PID and Linear Interpolation Lab 6: Orientation PID Lab 7: Kalman Filtering

Lab 8: Stunts!

TASK A: FLIP

UNDERSTANDING THE TASK:

The goal of this lab is to start the car on a line about 4 meters away from the wall, have it drive at full speed toward the wall until it reaches the sticky mat, then immediately reverse at full speed. The sudden change in direction should trigger the flip. After the flip, the car returns to the starting line.

GETTING STARTED:

To get started with this lab, I taped two 9V batteries to the front of my car to shift its center of mass forward. This helps the car generate more rotational momentum when it hits the mat, increasing the chances that it will successfully flip.

IMPLEMENTING THE CODE:

I followed a very similar strategy to that of my Lab 5 with the PID control. I did two case commands that would control the flippity flag I created. The second case command will also send the data after switching the flag.

Flippity Case Command

With that I then made a vinFlipDiesel() function that would make my car go at full speed towards the wall until it reaches 1200mm from the wall (this is just guess value to start off). I would then run full speed backwards to the starting line. This function was then called inside my loop() funciton.

VinFlipDiesel Function

Here is my loop() function:


void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to: ");
    Serial.println(central.address());

    while (central.connected()) {
      write_data();
      read_data();

      if (flippity){
        TOFSensor1.startRanging();
        vinFlipDiesel();
      }
      else {
        motors.stop();
      }
    } 

    Serial.println("Disconnected");
    motors.stop();
  }
}         
                    

DEBUGGING:

I ran into quite a few issues when testing my code. Here's a quick summary of the problems from the unsuccessful runs:

  1. The PWM value for my motors was too low, so the car didn’t move fast enough.
  2. My JST cable broke—twice.
  3. The limit distance from the wall was too large, which affected timing.
  4. The car wouldn't gain enough speed to flip.

To fix things, I adjusted the code and eventually increased the wall limit distance back to 1000 to give the car more room to build momentum.

This is the updated vinFlipDiesel() function:


void vinFlipDiesel(){
  bool triggered = false;
  float start = millis();
  
  while (true){
    if (TOFSensor1.checkForDataReady()){
      distance1 = TOFSensor1.getDistance();
      time_array[idx] = (millis() - start)/1000; //secs
      distance_array1[idx] = distance1 ; //mm

      motors.straightforward(250,0); 
      TOFSensor1.clearInterrupt();
      idx++;
      if (distance1 <= 1000) break;
    }
  }
  motors.straightbackward(250,0);
  time_array[idx] = (millis() - start)/1000; //secs
  delay(1000);

  motors.stop();
  idx++;

  TOFSensor1.stopRanging();
} 
                    

After multiple runs and hours of debugging and soldering I was able to get my first successful flip:

1st Flip not Straight

From this trial, I decided to add back the calibration factor I had previously removed so the car could go in a straight line. I also increased the factor to 1.8 because the path was still angled. I duct-taped the wheels to make it easier for the car to flip and I got the first fully successful run at 240 pwm.

1st Flip Straight

Here are the other two successful runs:

> 2nd Flip Straight
3rd Flip Straight

BLOOPERS:

REFERENCES:

For this lab I used a week extension and referenced Sarah Brown's page. I also used ChatGPT to help with the grammar.