Mars Rover Project code w/ Arduino

In summary, the conversation discusses multiple libraries for a Mars rover project, including code for line finding, sensor calibration, and motor and buzzer control. The conversation also mentions using the Serial.print() function to track and debug errors in the code. The mentor advises breaking down code changes into smaller chunks and using Serial.print() to verify changes and find errors.
  • #1
ParticleGinger6
32
5
Homework Statement
So I am in Advance Physics Lab II at my university and we are building a mars rover that will follow a black line on the ground. I have gotten that to work. I also have gotten the Rover to be able to avoid objects.

However, I also have to make the Mars Rover be able to sense gas (alcohol) and when the Rover goes by the gas the first time take in the readings and the second time, stop at the gas. I am having trouble with this section.

The Mars Rover is also supposed to sense a light source. Again the rover will drive the track and read in the light sensor value and on the second trip back will stop at the max value of the Rover

I have attached my three libraries of code but every time I try and fix one aspect of the code the parts that I have gotten to work tend to want to not work anymore.

I have been working on this project for a few weeks now. I am looking for some help because I seem to fix one problem just to create another 2 problems.
Relevant Equations
Arduino c++
Mentor note: I have removed author information and have enclosed the code below in code tags.
First library:
MarsRover_lineFinder2.ino
C:
#include <ZumoShield.h>
#include "MRlinelib.h"
#include <QTRSensors.h>
#include <Pushbutton.h>
#include <ZumoReflectanceSensorArray.h>
#include <ZumoMotors.h>
#include <ZumoBuzzer.h>
#define LED 11
MRlinelib motorControl;
Pushbutton button(ZUMO_BUTTON);
const int ProxiSensor = 6;
void setup() {
  Serial.begin(9600);
  button.waitForButton();
  motorControl.sensorCalibrate();
  pinMode(ProxiSensor, INPUT);
  button.waitForButton();
}
void loop() {
  if (digitalRead(ProxiSensor) == HIGH) {
    motorControl.followLine();
    motorControl.gasSensor();
    motorControl.lightSensor();
  }
  else {
    motorControl.proximitySensor();
  }
}

The Second Library:
MRlinelib.cpp
C:
#include "Arduino.h"
#include "MRlineLib.h"
#include <QTRSensors.h>
#include <Pushbutton.h>
#include <ZumoReflectanceSensorArray.h>
#include <ZumoMotors.h>
MRlinelib::MRlinelib(){};\
ZumoReflectanceSensorArray reflectanceSensors;
ZumoMotors motors;
ZumoMotors motorStop;
ZumoBuzzer buzzer;
int sensorPin = A0;
int lightSensorPin = A1;
int lightSensorValue;
int maxSpeed = 300;
int lastError = 0;
int maxSensor = 0;
int MRlinelib::sensorCalibrate(){
  reflectanceSensors.init();
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(1000);
  int i;
  for (i=0; i<80; i++){
    if ((i>10 && i <= 30)||(i>50 && i<=70))
      motors.setSpeeds(-200,200);
    else
      motors.setSpeeds(200,-200);
    reflectanceSensors.calibrate();
    delay(20);
  }
  motors.setSpeeds(0,0);
  digitalWrite(13, LOW);
  buzzer.play(">g32>>c32");
  buzzer.play("L16 cdegreg4");
  while(buzzer.isPlaying());
}

int MRlinelib::followLine(){
  unsigned int sensors[6];
  int position = reflectanceSensors.readLine(sensors);
  int error = position - 2500;
  int speedDifference = error / 4 + 6 * (error - lastError);
  lastError = error;
  int m1Speed = maxSpeed + speedDifference;
  int m2Speed = maxSpeed - speedDifference;
  if (m1Speed < 0)
    m1Speed = 0;
  if (m2Speed < 0)
    m2Speed = 0;
  if (m1Speed > maxSpeed)
    m1Speed = maxSpeed;
  if (m2Speed > maxSpeed)
    m2Speed = maxSpeed;
  motors.setSpeeds(m1Speed, m2Speed);
}

int MRlinelib::proximitySensor(){
  delay(50); 
  motors.setSpeeds(0,0);
  delay(500);
  motors.setSpeeds(-100,100);
  delay(660);
  motors.setSpeeds(0,0);
  delay(100);
  motors.setSpeeds(100, 100);
  delay(1000);
  motors.setSpeeds(100, -100);
  delay(660);
  motors.setSpeeds(0,0);
  delay(100);
  motors.setSpeeds(100, 100);
  delay(1500);
  motors.setSpeeds(100, -100);
  delay(660);
  motors.setSpeeds(0,0);
  delay(100);
  motors.setSpeeds(100, 100);
  delay(1000);
  motors.setSpeeds(0,0);
  delay(100);
  motors.setSpeeds(-100,100);
  delay(660);
  motors.setSpeeds(0,0);
  delay(50);
}

int MRlinelib::gasSensor(){
  int maxSensor =0;
  int k = 0;
  int sensorValue = analogRead(sensorPin);
  Serial.print("The sensor value is ");
  Serial.println(sensorValue);
  Serial.print("The k value is ");
  Serial.println(k);
  while(sensorValue > maxSensor){
    sensorValue = maxSensor;
  }
  if(sensorValue < maxSensor){
    k++;
    if(k >= 4){
      if(sensorValue == maxSensor){
        motors.setSpeeds(0,0);
      }
    }
  }
}

int MRlinelib::lightSensor(){
  int maxLightSensor;
  int j = 0;
  lightSensorValue = analogRead(lightSensorPin);
  while(lightSensorValue >= maxLightSensor){
    lightSensorValue = maxLightSensor;
  }
  if(lightSensorValue < maxLightSensor) {
    j++;
    if (j >=4){
      if(lightSensorValue == maxLightSensor) {
        motors.setSpeeds(0,0);
        delay(30000);
      }
    }
  }
}

The Third library:
MRlinelib.h
C:
#ifndef MRlinelib_h
#define MRlinelib_h
#include "Arduino.H"
#include <QTRSensors.h>
#include <Pushbutton.h>
#include <ZumoReflectanceSensorArray.h>
#include <ZumoMotors.h>
#include <ZumoBuzzer.h>
class MRlinelib {
  public: MRlinelib();
    int sensorCalibrate();
    int followLine();
    int proximitySensor();
    int gasSensor();
    int lightSensor();
  private:
};
#endif
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
These kinds of errors can be very hard to find and you will have to do a lot of testing and analysis of your code to find it.

Some examples of what can go wrong:

1) You forgot to initialize one or more pointers ie you forgot to assign a value to the pointer or forgot to malloc() memory for the pointer or defined insufficient space for the data.

2) A function defines a string or array locally and you pass it along in some function calls that overrun its size and since it's defined locally it's memory is on the stack and now you've over-written a part of the stack corrupting it meaning when you return from the lower function, it will crash or some variable's value will have changed.

You'll need to use Serial.print() to help you see what's going on inside by tracking your code at key points. As an example, at the entry and exit of each function print the inputs and returned outputs and verify that's what's expected. You must become one with the computer and think like the computer.

You have a controlling loop at the top. You could comment out the other calls and focus on the one you changed stepping it through the loop and looking at your data.

Try to do your program edits in much smaller chunks fixing one thing at a time even if its only a single line change and test the changes using Serial.print to verify it. Basically, don't try and fix multiple spots of your code as that can add new problems and hide the original problem.

Be aware that Serial.print() calls could mask the problem. I had one case where the program worked with my prints embedded and then failed when I took them out.

If you put a change in and it breaks then try removing the change and see if it works again. The change is doing something that's causing the problem to manifest. The change may or may not be the culprit but 9 times out of 10 it is.

Caveat: I've not programmed Arduino but have done PC-based C programming.
 
  • #3
I am having an error in my lineFollower() portion of my .cpp library. The error is that my Mars Rover is jolting back and forth but not constantly, what happens is the Mars Rover drives for like 4 inches than this jolt causes the robot to jump off the line then back onto the line. Since I have a turn around function in my code when position == 0 the code will cause the Rover to turn around and will not be able to complete the course.

I am also having issues with my gas Sensor which does read a value for the Alcohol, however, when the Rover gets back to the gas source I want the rover to stop but it does not stop.

I have not yet tried the light sensor but I believe that it will have the same issue.
 
  • #4
Focus on one problem at a time. Comment out the code that calls for a gas reading and light sensor reading and then see if it works correctly.

Are you printing out key values as it runs so you can see what's going on?
 
  • #5
@jedishrfu Yeah, I was printing out the sensorValue in gasSensor to make sure that I was converting the sensorValue = maxSensor correctly. That seemed to be working, but when I decided to test my k value, the k value was never incriminating.

But to the lineFollower function, why would that have gotten messed up without changing that, am I correct in guessing that since I added another function to when ProxiSensor == HIGH that the Rover would take a delay that I am not accounting for or is it something different from that. Maybe I will try Serial.println(position) to see why that is messing up.
 
  • #6
ParticleGinger6 said:
I am having an error in my lineFollower() portion of my .cpp library.
This line in your lineFollow() function jumped out at me as a possible source of one problem.
C:
int speedDifference = error / 4 + 6 * (error - lastError);

The expression on the right side is ##\frac{\text{error}} 4 + 6 \cdot (\text{error} - \text{ lastError})##
This might be what you intend, but if you really meant this --
##\frac{\text{error}} { 4 + 6 \cdot (\text{error} - \text{ lastError})}##
then you're missing a pair of parentheses around the entire denominator.

Also, as an alternative to using print statements as jedishrfu suggests, I would be using a debugger to single step through any functions that seem to be causing problems, and verifying that the results in intermediate calculations are correct.
 
  • #7
Regarding your gas sensor code, I'm not sure what you're trying to do. Here's part of that code:
C:
while(sensorValue > maxSensor){
    sensorValue = maxSensor;  
}
if(sensorValue < maxSensor){
    k++;
    if(k >= 4){
      if(sensorValue == maxSensor){
        motors.setSpeeds(0,0);
      } 
   }
}
The code before the while loop sets k to 0, and prints its value, but never prints any subsequent values.

The while loop probably doesn't do what you want it to do. It basically executes the loop body either once (if sensorValue > maxSensor) or not at all.

The if statement runs only once. Possibly you want this if statement to be part of the while loop, but that's not what you have. Adding more print statements or better yet, using a debugger, would be very helpful.
 
  • Like
Likes jedishrfu
  • #10
There is quite a lot wrong here I am afraid, you could do with a friend to show you how to do it and then you could see why her code works and yours doesn't. Here's some hints:
C:
int MRlinelib::gasSensor() {
  // You have already defined maxSensor in the global scope, you don't want to define
  // it again locally otherwise it won't be remembered after the current call to
  // gasSensor() returns.
  int maxSensor = 0;
  // Is k supposed to be the number of times you have seen gas? If so, call it something
  // more sensible than k. Do you think this should be declared locally here, or in the
  // global scope like maxSensor? REVISE VARIABLE SCOPING.
  int k = 0;
  int sensorValue = analogRead(sensorPin);
  Serial.print("The sensor value is ");
  Serial.println(sensorValue);
  Serial.print("The k value is ");
  Serial.println(k);

  // Using a while makes no sense here, you should use an if. REVISE WHILE.
  while (sensorValue > maxSensor) {
    // Are you sure this is the right way round?
    sensorValue = maxSensor;
  }
  // Do you really want to do something special when the value is low?
  if (sensorValue < maxSensor) {
    // This is presumably meant to count the number of times the sensor value has
    // increased above the max, but why the magic number 4?
    k++;
    if (k >= 4) {
      // Why are you comparing the value again? Also, never compare an analog value
      // using == as this implies that the reading must be exactly the same to the level
      // of resolution of the analog input (typically 0.025%).
      if (sensorValue == maxSensor) {
        // This will stop the motors, but what will happen next?
        motors.setSpeeds(0,0);
      }
    }
  }
}
[edited for line length/readability]
 
  • Like
Likes jedishrfu

What is the "Mars Rover Project code w/ Arduino"?

The "Mars Rover Project code w/ Arduino" is a coding project that utilizes the Arduino platform to program a rover that can simulate the movement and data collection of a rover on Mars.

What is Arduino?

Arduino is an open-source electronics platform that allows for the creation of interactive projects through coding and hardware components. It is commonly used for robotics, IoT, and other electronic projects.

How does the Mars Rover Project code work?

The Mars Rover Project code utilizes the Arduino platform to control the movement and data collection of a rover. This is done through writing code that commands the rover's motors, sensors, and other components. The code can be customized and modified to fit specific project requirements.

What are the benefits of using Arduino for the Mars Rover Project?

Arduino provides a user-friendly platform for coding and hardware integration, making it accessible for beginners and experts alike. It also offers a wide range of sensors and components that can be used for the rover, making it a versatile and customizable option for the project. Additionally, Arduino has a large community of users, providing support and resources for troubleshooting and further development.

Can the Mars Rover Project code be used for other projects?

Yes, the Mars Rover Project code can be adapted and modified for other projects that require similar coding and hardware components. It can be a great learning tool for exploring robotics, programming, and electronics.

Similar threads

Replies
22
Views
2K
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Computing and Technology
Replies
1
Views
6K
Replies
1
Views
3K
  • Electrical Engineering
Replies
5
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top