Skip to content

Instantly share code, notes, and snippets.

@adityakamath
Created June 16, 2016 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adityakamath/f571aa1136bf3cdb8bca5b76cef5dd07 to your computer and use it in GitHub Desktop.
Save adityakamath/f571aa1136bf3cdb8bca5b76cef5dd07 to your computer and use it in GitHub Desktop.
random_walk function
void random_walk() {
int random_choice = 0;
io.readOdometryData(odom);
if ((scan.ranges[166] <= radius_value && scan.ranges[833] <= radius_value)) {
flag_corridor = 1; // still in the corridor
}
else {
flag_crossroad = 1; // in the crossroad
}
if (!transition) { // if the robot is not in a junction, take decision
transition = 1;
if (dead_detect()) decision_variable = 4; // dead end
else if (open_space_detect()) {
if (scan.ranges[166] >= scan.ranges[833]) {
decision_variable = 5; // left is closer
// next_decision = 1; // next decision must be left
}
else {
decision_variable = 6; // right is closer
// next_decision = 2; // next decision must be right
}
}
else if (straight) {
//if (straight) {
if (left_turn) {
if (right_turn) {
// straight, left and right
random_choice = random(1, 1, 1);
if (random_choice == 1) decision_variable = 1; // pick left
else if (random_choice == 2) decision_variable = 2; // pick right
else decision_variable = 3; // pick straight
}
else {
// straight and left
random_choice = random(1, 1, 0);
if (random_choice == 1) decision_variable = 1; // pick left
else decision_variable = 3; // pick straight
}
}
else {
if (right_turn) {
// straight and right
random_choice = random(1, 1, 0);
if (random_choice == 1) decision_variable = 2; // pick right
else decision_variable = 3; // pick straight
}
else {
// straight
decision_variable = 3; // pick straight automatically
}
}
}
else if (left_turn) {
if (right_turn) {
// left and right
random_choice = random(1, 1, 0);
if (random_choice == 1) decision_variable = 1; // pick left
else decision_variable = 2; // pick right
}
else {
// left
decision_variable = 1; // pick left automatically
}
}
else if (right_turn) {
// right
decision_variable = 2; // pick right automatically
}
if (decision_variable == 1) {
cout << "Going left!" << endl;
turning_left = 1;
turn = 1;
left_stuck = 1;
right_stuck = 0;
straight_stuck = 0;
// Alternative way of sticking to the decision !!!!!!
io.readOdometryData(odom);
a = odom.a; // current angle
}
else if (decision_variable == 2) {
cout << "Going right!" << endl;
turning_right = 1;
turn = 1;
right_stuck = 1;
left_stuck = 0;
straight_stuck = 0;
// Alternative way of sticking to the decision !!!!!!
io.readOdometryData(odom);
a = odom.a; // current angle
}
else if (decision_variable == 3) {
cout << "Going straight!" << endl;
going_straight = 1;
turn = 0;
straight_stuck = 1;
right_stuck = 0;
left_stuck = 0;
}
else if (decision_variable == 4) {
cout << "Dead end or door?" << endl;
dead_end = 1;
straight_stuck = 0;
right_stuck = 0;
left_stuck = 0;
}
else if (decision_variable == 5) {
cout << "Open space!" << endl;
open_space = 1;
straight_stuck = 0;
right_stuck = 0;
left_stuck = 0;
}
else if (decision_variable == 6) {
cout << "Open space!" << endl;
open_space = 1;
straight_stuck = 0;
right_stuck = 0;
left_stuck = 0;
}
else { // if it picks an unavailable choice!
cout << "No pick!" << endl;
}
// cout << decision_variable << endl;
/*if (decision_variable == 1 || decision_variable == 2) history = decision_variable;
else history = 2;
*/
}
// Conditions to exit the transition (junction) phase!!!!!
else if (turning_left && (!((a < pi / 2 && ((odom.a - a) < pi / 2)) || (a >= pi / 2 && (((odom.a - a) < -3 * pi / 2) || (abs(odom.a - a) < pi / 2))))) ||
(turning_right && !((a > -pi / 2 && ((odom.a - a) > -pi / 2)) || (a <= -pi / 2 && (((odom.a - a) > 3 * pi / 2) || (abs(odom.a - a) < pi / 2))))) ||
(flag_corridor && flag_crossroad && (scan.ranges[166] <= radius_value && scan.ranges[833] <= radius_value) && going_straight) ||
((scan.ranges[166] <= radius_value && scan.ranges[833] <= radius_value) && going_straight) ||
(dead_detect() && !inspect) ||
(open_space_detect()) ||
(!open_space_detect() && open_space) ||
((clock() / CLOCKS_PER_SEC - t_change) > 5)) {
transition = 0; // the robot is in the corridor again so it can make a new decision
// reset the flags
straight = 0; left_turn = 0; right_turn = 0;
dead_end = 0; open_space = 0;
turning_left = 0; turning_right = 0; going_straight = 0;
flag_corridor = 0;
flag_crossroad = 0;
left_door = 0; right_door = 0;
if ((clock() / CLOCKS_PER_SEC - t_change) > 5) cout << "Long time no decision!" << endl;
t_change = clock() / CLOCKS_PER_SEC; // update timer
cout << "Time to take another decision!" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment