/*
Copyright (c) 2005, Brad Kratochvil, Toby Collett, Brian Gerkey, Andrew Howard, ...
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the Player Project nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

 // Example modified by zjb Feb 2011 to use RangerProxy instead of Sonar.

#include <iostream>
#include <libplayerc++/playerc++.h>
#include <iomanip> // for setw

int
main(int argc, char *argv[])
{
  using namespace PlayerCc;

  PlayerClient    robot(argv[1]);
  RangerProxy      rp(&robot,0);
  Position2dProxy pp(&robot,0);

  std::cout << "Instantiated proxies" << std::endl;

  for(;;)
  {
    double turnrate, speed;

    // read from the proxies
    robot.Read();

    std::cout << "Got data " << rp.GetRangeCount() << std::endl;

    std::cout << "Pos: (" << pp.GetXPos() << "," << pp.GetYPos() << ")" << std::endl; 

    // print out some laser data for fun
    for (int r = 0; r < rp.GetRangeCount(); r+= 80) {
      std::cout << std::setw(7) << rp[r] << " ";
    }
    std::cout << std::endl;

    // leftmost is 30 degrees behind us.
    // directly left is 30/(360/1024) = 85
    // directly right is 210/(360/1024) = 597
    // do simple collision avoidance
    double leftval = (rp[85]+rp[90])/2.0;
    double rightval = (rp[592]+rp[597])/2.0;
    double frontval = (rp[340]+rp[345])/2.0;

    /*
    if(leftval < rightval)
      turnrate = dtor(-20); // turn 20 degrees per second
    else
      turnrate = dtor(20);
    */

    if(leftval < 1.0)
      turnrate = dtor(-20); // turn 20 degrees per second
    else if (rightval < 1.0)
      turnrate = dtor(20);

    if(frontval < 0.500) {
      speed = 0;
      if(leftval > rightval)
	turnrate = dtor(-20); // turn 20 degrees per second
      else
	turnrate = dtor(20);
    }
    else
      speed = 0.100;

    // command the motors
    pp.SetSpeed(speed, turnrate);
  }
}

