DIT112-V20 / group-05

MIT License
1 stars 0 forks source link

Sensor is not responding #138

Closed JakobRoseke closed 4 years ago

JakobRoseke commented 4 years ago

@platisd Hi again... hopefully last time I will have to ask for some answers.

In the branch, 'finalizeCar' -> src/smartCar/smartCar, the sensor won't give any reading other than 0.. The sensor works fine when we use this code;

#include <Smartcar.h>

const int FRONT_TRIGGER_PIN = 4; 
const int FRONT_ECHO_PIN = 2; 

//SMARTCAR VARIABLES
const unsigned int FRONT_MAX_DISTANCE = 100;
SR04 front_sensor(FRONT_TRIGGER_PIN, FRONT_ECHO_PIN, FRONT_MAX_DISTANCE);
BrushedMotor leftMotor(smartcarlib::pins::v2::leftMotorPins);
BrushedMotor rightMotor(smartcarlib::pins::v2::rightMotorPins);
DifferentialControl control(leftMotor, rightMotor);
GY50 gyroscope(2);
const auto pulsesPerMeter = 600;
DirectionlessOdometer leftOdometer(
    smartcarlib::pins::v2::leftOdometerPin, []() { leftOdometer.update(); }, pulsesPerMeter);
DirectionlessOdometer rightOdometer(
    smartcarlib::pins::v2::rightOdometerPin, []() { rightOdometer.update(); }, pulsesPerMeter);  

//INITIALIZE THE SMARTCAR
SmartCar car(control, gyroscope, leftOdometer, rightOdometer);

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
    car.update();
    int distance = front_sensor.getDistance();
    Serial.print(distance);
    Serial.println(": Distance to obstacle");
}

I do not know why it won't work in our regular code!! Do you see anything that would indicate it not working as it should?

platisd commented 4 years ago
  1. Which line of code exhibits this behavior? Link to it.
  2. Did this sensor used to behave as expected before? If yes, when was that?
JakobRoseke commented 4 years ago
  1. Which line of code exhibits this behavior? Link to it.
  2. Did this sensor used to behave as expected before? If yes, when was that?
  1. I don't quite understand the question.. The code I have posted in the issue gives a correct reading from the sensor, i.e it outputs more than a 0 when I hover an object in front of the car. https://github.com/DIT112-V20/group-05/blob/finalizeCar/src/smartCar/smartCar.ino this is the code that always gives a 0 in reading.

  2. Yes, the sensor/car worked fine used last Saturday and we did not really change anything in the code up until yesterday. As I said, the sensor still works with the code posted in the issue, but whenever I upload the code that is in branch 'finalizeCar' -> src/smartCar/smartCar.ino the reading ALWAYS gives a 0.

platisd commented 4 years ago

Please provide a link to the line(s) of code that have the problem. front_sensor is not used at only one place.

JakobRoseke commented 4 years ago

Please provide a link to the line(s) of code that have the problem. front_sensor is not used at only one place.

https://github.com/DIT112-V20/group-05/blob/2dcf352bb308fdf4ffa6c4e2672145e766db13a1/src/smartCar/smartCar.ino#L85-L116

platisd commented 4 years ago

Use a different pin for FRONT_ECHO_PIN if you want to also keep using the on-board LED. LED_BUILTIN is a constant that has the value of 2 in the ESP32 DOIT devkit v1 board that you have. In other words, pin D2 is also connected to the on-board LED.

Software-wise, the "echo" pin of a SR04 sensor has to be an INPUT. Setting this pin to an input, when provided to an SR04 constructor is taken care of by the Smartcar library. In your case you set its mode/direction to be an OUTPUT in setup() and then controlling its state, which clearly messes the measurements up.

JakobRoseke commented 4 years ago

Thank you very much, it works now!

I changed the echo pin from 2 to 16 and added pinMode for the echo and trigger.

platisd commented 4 years ago

added pinMode for the echo and trigger.

No, you shouldn't (or rather don't need to do this) since as I mentioned it is taken care of by the Smartcar library.

JakobRoseke commented 4 years ago

ahh okay, it worked anyways but I will remove them.