Testing in procedural programming code
https://softwareengineering.stackexchange.com/questions/236758
-
03-10-2020 - |
Question
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;
}
Solution
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.