Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Myro Behaviors

By: a guest on Sep 27th, 2012  |  syntax: C++  |  size: 1.98 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Code by Graham Gier, USC ID# 7227272017
  2.  
  3. #include <Myro.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10. double ambient;
  11.  
  12. double normalize(int v) {
  13.     if (v>ambient)
  14.     {
  15.         v=ambient;
  16.     }
  17. return 1.0-v/ambient;
  18. }
  19.  
  20. void alive()
  21. {
  22.     ambient = (robot.getLight("left") +
  23.                robot.getLight("center") +
  24.                robot.getLight("right")) / 3.0;
  25.     // Braitenberg vehicle#1: Alive
  26.      while (true) {
  27.         int L = robot.getLight("center");
  28.         robot.forward(normalize(L)); }
  29.     disconnect();
  30. }
  31.  
  32. void coward()
  33. {
  34.     ambient = (robot.getLight("left") +
  35.                robot.getLight("center") +
  36.                robot.getLight("right")) / 3.0;
  37.     while (true) {
  38.         int L = robot.getLight("left");
  39.         int R = robot.getLight("right"); robot.motors(normalize(L), normalize(R));
  40.     }
  41.     disconnect();
  42. }
  43.  
  44. void aggressive()
  45. {
  46.     ambient = (robot.getLight("left") +
  47.                robot.getLight("center") +
  48.                robot.getLight("right")) / 3.0;
  49.     while (true) {
  50.         int R = robot.getLight("right");
  51.         int L = robot.getLight("left"); robot.motors(normalize(R), normalize(L));
  52.     }
  53.     disconnect();
  54. }
  55.  
  56. void indecisive()
  57. {
  58.     ambient = (robot.getLight("left") +
  59.                robot.getLight("center") +
  60.                robot.getLight("right")) / 3.0;
  61.   while(true)
  62.   {
  63.       if (robot.getLight("center") <= ambient)
  64.           robot.forward(1, 1);
  65.       else robot.backward(1,1);
  66.   }
  67.    
  68. }
  69.  
  70. int main()
  71. {
  72.     connect("/dev/tty.Fluke2-0221-Fluke2");
  73.     cout<<"choose a behavior"<<endl;
  74.     cout<<"alive= 1"<<endl;
  75.     cout<<"coward= 2"<<endl;
  76.     cout << "aggressive= 3"<< endl;
  77.     cout << "indecisive= 4" << endl;
  78.     int input;
  79.     cin >> input;
  80.     if (input == 1)   alive();
  81.     else if (input ==2) coward();
  82.     else if (input == 3) aggressive();
  83.     else if (input == 4) indecisive();
  84.         disconnect();
  85.         return 0;
  86.     }