// Jet
// TAB FILE > JAvarsCons49.h -> JAvarsCons54.h

/*== NEW ====================================================
//Jet
const byte pacebyScale =4 ; //Servodegrees = pacebyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
const byte turnbyScale =4 ; //Servodegrees = turnbyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
const byte rollbyScale =4 ; //Servodegrees = rollbyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
//== CHANGED=================================================

*/
//===========================================================
//EEPROM addresses
const byte E8f_walking   =255;   //31 - 0,1  init -> f_walking
const byte E8rollC       =254;   //30 - init -> rollC
const byte E8paceC       =253;   //29 - init -> paceC
const byte E8rollby      =252;   //28 - init -> rollby
const byte E8paceby      =251;   //27 - init -> paceby, turnby
const byte E8rollmin     =250;   //26 - nominal minimum roll value, not used at present   
const byte E8rollmax     =249;   //25 - nominal maximum roll value, not used at present  
const byte E8pacemin     =248;   //24 - nominal minimum roll value, not used at present  
const byte E8pacemax     =247;   //23 - nominal maximum roll value, not used at present   
const byte E8rgroup      =246;   //22 - 'A-Z       init(A) -> rgroup
const byte E8rname       =245;   //21 - '0-9'      init(0) -> rname 
const byte E8autoAct     =244;   //20 - '0-8' 
const byte E8SonarAwareAt =243;  //19 - 1-9  init(5) -> SonarAwareAt
const byte E8SonarTooNear =242;  //18 - 1-9  init(2) -> SonarTooNear
const byte E8f_cm         =241;  //17 - true, false  init
const byte E8rollspeedmax =240;  //16 - init
const byte E8pacespeedmax =239;  //15 - init
const byte E8rollspeed    =238;  //14 - 1-9  init
const byte E8pacespeed    =237;  //13 - 1-9  init
const byte E8Greact       =236;  //12 - 0,1  Ground     react
const byte E8Wreact       =235;  //11 - 0,1  Whiskers   react
const byte E8Ureact       =234;  //10 - 0,1  Ultrasonic react
const byte E8AutoSense    =233;  //10 - 0,1  
// 224 is default behaviour marker

//addresses within a behaviour slot, base is BehaviourEEbase
const byte Es31f_walking    =31;
const byte Es30rollC        =30;   // adjust servos
const byte Es29paceC        =29;   // adjust servos
const byte Es28rollby       =28;   // adjust servos
const byte Es27paceby       =27;   // adjust servos
const byte Es26rollmin      =26;   // adjust servos but never used
const byte Es25rollmax      =25;   // adjust servos but never used
const byte Es24pacemin      =24;   // adjust servos but never used
const byte Es23pacemax      =23;   // adjust servos but never used
const byte Es22rgroup       =22;   // 'bs' via 'b#'   
const byte Es21rname        =21;   // 'bs' via 'b#'
const byte Es20autoAct      =20;   // 'bs' via 'ba'
const byte Es19SonarAwareAt =19;   // 'bs' via 'bus'
const byte Es18SonarTooNear =18;   // 'bs' via 'bus'
const byte Es17f_cm         =17;   // 'bs' via 'bc0','bc1'	
const byte Es16rollspeedmax =16;   // 'bs' via 'sm','srm'
const byte Es15pacespeedmax =15;   // 'bs' via 'sm','spm'
const byte Es14rollspeed    =14;   // 'bs' via 'sr'
const byte Es13pacespeed    =13;   // 'bs' via 'sp'
const byte Es12Greact       =12;   // 'bs' via 'bG'
const byte Es11Wreact       =11;   // 'bs' via 'bW'
const byte Es10Ureact       =10;   // 'bs' via 'bU'
const byte Es9AutoSense     =9;    // 'bs' via 'b$'
//                            0 is default behaviour marker ' ','@'

//== VARIABLES and CONSTANTS ================================ 
//BEHAVIOUR
char autoAct ='0' ;
byte SonarTooNear ;   // init =EEPROM(2), 'bu(m)-', units 50mm/2inch, objects nearer will be considered as Too Near and avoided  
byte SonarAwareAt ;   // init =EEPROM(5), 'bu(m)+', units 50mm/2inch, objects nearer will avoided, objects farther away will be ignored
/*these behaviour variable are declared below
const byte Servoframe =20; // Servo frame rate millisecs
const byte maxroll =50;    // physically max roll         //Jet
const byte maxpace =50;    // physically max pace         //Jet
byte rollC =90;      // roll centre = 90 degrees (ie 1500us) => upright
byte paceC =90;      // pace centre = 90 degrees (ie 1500us) => feet together
byte rollby ;        // roll angles about rollC
byte paceby ;        // pace angles about paceC
byte turnby;         // servo turn angles about paceC
byte rollspeed ;     // roll servo speed  
byte pacespeed ;     // pace servo speed
byte rollspeedmax ;  // max speed for speedup()
byte pacespeedmax ;  // max speed for speedup()
*/
//MIND                       
boolean f_walking ;       // 1=> walking enabled , 0=> walking disabled
boolean f_bv =0;          //verbose, light level reporting of sensors, 0=off, 1=on
boolean f_BeQuiet ;       // 1-> status messages off
boolean f_MsgForMe ;      // 0-> messages are for other robots 
boolean f_hashcmnd    =0 ;// processing a hash command
boolean f_hashhashSTOP ;  // ##Stop from external commander
boolean f_TurnedRight ;   // 1=> last turn was Right, 0=> last turn was Left 
boolean f_forwards    =1 ;// current direction, 1=>forwards, 0=>backwards, controls step direction in turns
boolean f_turning     =0 ;// controls pace in paceRightForward()) and paceRightBackwards() when turning,   f_turning =1 =>turning
boolean f_AtLight ;       // true if facing Light
boolean f_AtDark ;        // true if facing Dark
boolean f_holeRight ;     // 0=> no hole, 1=> hole eg a table edge,  
boolean f_holeLeft ;      // 0=> no hole, 1=> hole eg a table edge
boolean f_obstacleRight ; // 0=> no obstacle, 1=> obstacle 
boolean f_obstacleLeft ;  // 0=> no obstacle, 1=> obstacle
boolean f_doDefault =true;    //true for the first loop and #< only
boolean f_bG =0;              //Ground react control
boolean f_bW =0;              //Whiskers react control
boolean f_bU =0;              //Ultrasonic react control
boolean f_bAutoSense =0;      //auto sense before every command except when recording or in a #command, b$0=off, b$1=on
boolean f_hashwalking ;       //saved value during ##
boolean f_hashbAutoSense;     //saved value during ##
boolean f_hashrunDefault;     //saved value during ##

byte cmnd =0 ;            // current command     
char rname ;         // robot name
char rgroup ;        // robot group
//SERVOS
//Servo values are in units of 1 degree, 0 - 180, centre at 90"
const byte Servoframe =20; // Servo frame rate millisecs
const byte maxroll =60;    // physically max roll         //Jet horn inline with tilt wire
const byte maxpace =60;    // physically max pace         //Jet tilt wires hit legs
byte rollC =90;      // roll centre = 90 degrees (ie 1500us) => upright
byte paceC =90;      // pace centre = 90 degrees (ie 1500us) => feet together
byte rollat ;        // Roll servo position, +ve roll to right 
byte paceat ;        // Pace servo position, +ve left foot forwards
byte rollby ;        // roll angles about rollC
byte paceby ;        // pace angles about paceC
byte turnby;         // servo turn angles about paceC
byte rollspeed ;     // roll servo speed  
byte pacespeed ;     // pace servo speed
byte rollspeedmax ;  // max speed for speedup()
byte pacespeedmax ;  // max speed for speedup()
byte rollto ;        // roll servo value to move to
byte paceto ;        // pace servo value to move to
byte Srollat ;       // remember rollat when walking disabled
byte Spaceat ;       // remember paceat when walking disabled
//Bambino
/*
const byte pacebyScale =2 ; //Servodegrees = pacebyCmnd *pacebyScale, 2 gives a maximum of 18, default 15
const byte turnbyScale =2 ; //Servodegrees = turnbyCmnd *pacebyScale, 2 gives a maximum of 18, default 15
const byte rollbyScale =2 ; //Servodegrees = rollbyCmnd *pacebyScale, 2 gives a maximum of 18, default 15
*/
//Jet
const byte pacebyScale =4 ; //Servodegrees = pacebyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
const byte turnbyScale =4 ; //Servodegrees = turnbyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
const byte rollbyScale =4 ; //Servodegrees = rollbyCmnd *pacebyScale, 4 gives a maximum of 36, default 15
//== SENSORS ================================================
//Eyes
int ReyeLight ;      // light level read from R whisker light sensors, integer values between 0 and 1023
int LeyeLight ;      // light level read from L whisker light sensors, integer values between 0 and 1023
const byte LeftRightLightTurnThreshold  =10 ;  // difference% between LeyeLight and ReyeLight for a Light turn
//===========================================================
//Whiskers
int RwhiskerLight;   // reading from Right whisker sensor, LED on,  integer values between 0 and 1023
int LwhiskerLight ;  // reading from Left whisker sensor,  LED on,  integer values between 0 and 1023
int RwhiskerDark ;   // reading from Right whisker sensor, LED off, integer values between 0 and 1023
int LwhiskerDark ;   // reading from Left whisker sensor,  LED off, integer values between 0 and 1023
int RwhiskerDifference ; // difference between RwhiskerLight and RwhiskerDark
int LwhiskerDifference ; // difference between LwhiskerLight and LwhiskerDark
const byte obstacleThresholdRight =20 ;  // difference between RwhiskerLight and RwhiskerDark for an obstacle
const byte obstacleThresholdLeft  =20 ;  // difference between LwhiskerLight and LwhiskerDark for an obstacle
//===========================================================
#ifdef Jet
//ToeWhiskers           //54
int RtoeWhiskerLight ;    // reading from Right beard sensor, LED on,  integer values between 0 and 1023
int LtoeWhiskerLight ;    // reading from Left beard sensor,  LED on,  integer values between 0 and 1023
int RtoeWhiskerDark ;     // reading from Right beard sensor, LED off, integer values between 0 and 1023
int LtoeWhiskerDark ;     // reading from Left beard sensor,  LED off, integer values between 0 and 1023
int RtoeWhiskerDifference ; // difference between RbeardLight and RbeardDark
int LtoeWhiskerDifference ; // difference between LbeardLight and LbeardDark
const byte toeObstacleThresholdRight =40 ;      // difference between RtoeWhiskerLight and RtoeWhiskerDark for an obstacle
const byte toeObstacleThresholdLeft  =40 ;      // difference between LtoeWhiskerLight and LtoeWhiskerDark for an obstacle 
//===========================================================
#else
//Beard
int RbeardLight ;    // reading from Right beard sensor, LED on,  integer values between 0 and 1023
int LbeardLight ;    // reading from Left beard sensor,  LED on,  integer values between 0 and 1023
int RbeardDark ;     // reading from Right beard sensor, LED off, integer values between 0 and 1023
int LbeardDark ;     // reading from Left beard sensor,  LED off, integer values between 0 and 1023
int RbeardDifference ; // difference between RbeardLight and RbeardDark
int LbeardDifference ; // difference between LbeardLight and LbeardDark
const byte holeThresholdRight =10 ;      // difference between RbeardLight and RbeardDark for a hole
const byte holeThresholdLeft  =10 ;      // difference between LbeardLight and LbeardDark for a hole 
#endif
//===========================================================
// Ultrasonic module HC-SR04
  // According to Parallax's datasheet for the PING))), 
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  // sound travels at 1130 feet per second, i.e. 73.746 microseconds per inch
  // (or the speed of sound is 340 m/s or 29 microseconds per centimeter).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle
  // and then divide by 29 to get cm or divide by 73.746 to get inches,
  // i.e. divide by 2*29=58 to get cm, 
  // or divide by 2*73.746=147.492 =>147 to get inches.
  // Note the output of the HC_SR04 never goes LOW unless an echo is received.
  // The default time-out for pulseIn() is 1 second (Arduino language reference).
  // so if there is no obstacle to return an echo then unless a timeout is set
  // pulseIn() will return 0 after its timer times out and this routine 
  // will hang for 1 second.
  // The HC-SR04 has a range of about 10 feet, then since the speed of sound
  // is about 1 foot per milli-second it will take 20ms at maximum range.
  // so UStimeout is set to 20000 =20ms
const int UStimeout =20000;     //20ms
const byte uscm     =58;
const byte usinch   =147;
boolean f_cm        =true;      // US distance in cm
int UStime;       //pulsein usec
int Udist;       //conversion cm or inch 
byte objectRange ;    // 2"/5cm distance range to object detected by HC-SR04 sensor 
//===========================================================
