Program the robot to do the following two tasks simultaneously: Task 1: Play the
ID: 3586890 • Letter: P
Question
Program the robot to do the following two tasks simultaneously: Task 1: Play the tune (G/w.Ew-G/w-MwD/h-Eh, w: whole note, h: ½ note) Task 2: Move forward. Whenever it bumps into something, it backs up and then turns exactly 4 times. 90° either left or right in random. Then it keeps moving forward in the nevw direction. Repeatedly do this, but upon the 4th time it hits an obstacle, it should come to a full stop without turning Note: you must make use of the loop icons for both tasks. Describe your algorithm as two sequences of steps that will run simultaneously hExplanation / Answer
Please save the below code in a CPP file, compile and run. You can modify as per your requirement
/***************************/
/*
* Robot.cpp
*
* Created on: 05-Oct-2017
* Author:
*/
#include <iostream>
#include <stdlib.h>
bool tunestatus; // this boolean variable shall be true if any tune is playing
bool bumps; // this boolean variable shall be true if any bumps found
int turncount;
bool stopflag;
void ForwardMove();
enum tune{
NOTUNE,
CW,
EW,
GW,
AW,
DH,
FH
}NOTE;
enum move{
NOMOVE,
FORWARD,
BACKWARD
}movement;
enum rotate{
NOROTATE,
LEFT,
RIGHT
}rotation;
void PlayTune(int n)
{
tunestatus=true;
switch(n)
{
case 1:
NOTE=CW;
break;
case 2:
NOTE=EW;
break;
case 3:
NOTE=GW;
break;
case 4:
NOTE=AW;
break;
case 5:
NOTE=DH;
break;
case 6:
NOTE=FH;
break;
defaut:
NOTE=NOTUNE;
break;
}
}
int main()
{
int tune_no=0;
int counter=0;
turncount=0;
NOTE=NOTUNE;
movement=NOMOVE;
rotation=NOROTATE;
stopflag=false;
tunestatus=false;
bumps=false; // Thiscan be updated outside
while(1)
{
//playing tune
if(tunestatus==false)
{
PlayTune(tune_no+1);
tune_no++;
tune_no=tune_no%7;
}
else
{
if(counter>100)//modify this condition for play timing
{
counter=0;
tunestatus=false;
}
}
counter++;
//forward movement
if(stopflag==false)
{
ForwardMove();
}
}
}
void ForwardMove()
{
if(bumps==true)
{
turncount++;
if(turncount==4)
{
stopflag=true;
return; //at 4th bump function shall return from here making stopflag true. so this function
// will not be called again
}
movement=BACKWARD;
int r=rand(); // generate random number to decide random rotation
if(r%2)
rotation=LEFT;
else
rotation=RIGHT;
}
movement=FORWARD;
rotation=NOROTATE;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.