I have designed and implemented, for my final year in college, an autonomous robot with Arduino board microcontroller. The robot wanders around an area, avoids obstacles and tries to detect intruders.
I have to do some testing for this project . Could anyone help me by telling what test procedures I can perform in procedural code?
Observation of the behavior of robot could be one of them?
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
const int trigPin = 5;
const int echoPin = 2;
int buzzerPin = 6; // choose the pin for the LED //buzzer
int inputPin = 9; // choose the input pin (for PIR sensor)
int val = 0;
Timer t;
boolean pirSense = false;
long xronos=0;
void setup() {
Serial.begin(9600);
servoLeft.attach(4); // Set left servo to digital pin 10
servoRight.attach(3); // Set right servo to digital pin 9
pinMode(buzzerPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
t.every(30000,monitoring);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
distance = microsecondsToCentimeters(duration);
delay(100);
Serial.println(distance);
if (distance >= 40){
moveForward();
}
else {
stopMove();
int x = random(0,2);
if(x == 0 ){
turnRight();
delay(500);
}
else{
turnLeft();
delay(500);
}
}
t.update();
}
void monitoring(){
xronos = now();
while( xronos+5 >now() ){
stopMove();
delay(1000);
pirSensor();
}
}
// Motion routines for forward, reverse, turns, and stop
void reverse() {
servoLeft.write(0);
servoRight.write(180);
}
void moveForward() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopMove() {
servoLeft.write(90);
servoRight.write(90);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
-
1Do you have pure (side-effect-less) procedures? If so, you can write regular unit tests for those.Useless– Useless2014年04月23日 15:23:55 +00:00Commented Apr 23, 2014 at 15:23
-
I mean functions which take arguments, return results, and don't modify any global variables or other external state (ie, they don't move the robot, print a string, etc.)Useless– Useless2014年04月23日 15:27:07 +00:00Commented Apr 23, 2014 at 15:27
-
I have only one function that takes an argument and return something into the main program. All the others are void type. Also i have 2 global variables that could change their value!alexca– alexca2014年04月23日 15:30:57 +00:00Commented Apr 23, 2014 at 15:30
-
The term you're looking for is "unit testing." You unit test methods by calling them and seeing if they return the expected value, or modify the expected variables.Robert Harvey– Robert Harvey2014年04月23日 15:33:27 +00:00Commented Apr 23, 2014 at 15:33
-
You think that i could say something about observation of the behavior of the robot for the testing?alexca– alexca2014年04月23日 15:34:46 +00:00Commented Apr 23, 2014 at 15:34
1 Answer 1
Observation of the robot is probably the most important test you can run. Don't let anyone tell you that anything short of a full integration test (which is what your observation is) is fully testing.
However, there are also unit testing frameworks for Arduino.
There are also Pseudo-Arduino boards.
And there are also ways to do pseudo-object-oriented C, to allow you to test your objects independently.
All of these will probably serve you well in a variety of contexts. Understand your options and pick the best tool for the job; that's the skill of programming.
But never forget that there's no better testing than watching the totality of your code work as it's intended. That applies as much to writing a web page, or a game or a robot controller.
-
Thanks for your answer! For the pseudo-Arduino in perl i downloaded the file from github. Now i have to downolad some IDE for perl in order to run it?alexca– alexca2014年04月23日 15:59:12 +00:00Commented Apr 23, 2014 at 15:59
-
@user2250119: perl.org/get.htmlpdr– pdr2014年04月23日 16:13:31 +00:00Commented Apr 23, 2014 at 16:13