view main/robots/odr/NavigateTask.cpp @ 221:8e6ca2ead01a main

Latest updates.
author Bob Cook <bob@bobcookdev.com>
date Mon, 09 Sep 2013 20:03:31 -0700
parents e23198d929e9
children 1d246b3260c4
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/odr/NavigateTask.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Subsumption task to drive forward in a particular heading.
//
//  Copyright (c) 2013 Bob Cook
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#include "NavigateTask.h"

#include <cmath>

#include <Poco/Logger.h>
#include <Poco/NumberFormatter.h>

#include "MotorsAndServos.h"
#include "Scoreboard.h"

// ----------------------------------------------------------------------------------------

static double ComputeHeadingDiff( double target, double current )
{
    if ( target >= 0.0 && current >= 0.0 )
    {
        return target - current;
    }
    else if ( target < 0.0 && current < 0.0 )
    {
        return target - current;
    }
    else
    {
        double diffFromZero = std::abs( target ) + std::abs( current );
        double diffFrom180  = 360.0 - diffFromZero;

        if ( diffFromZero < diffFrom180 )
        {
            if ( current < 0.0 ) diffFromZero *= -1.0;
            return diffFromZero;
        }
        else
        {
            if ( current < 0.0 ) diffFrom180 *= -1.0;
            return diffFrom180;
        }
    }

    return 0.0;
}

// ----------------------------------------------------------------------------------------

NavigateTask::NavigateTask( const std::string& loggerName )
    : TaskObject( loggerName )
{
}

// ----------------------------------------------------------------------------------------

NavigateTask::~NavigateTask()
{
}

// ----------------------------------------------------------------------------------------

void NavigateTask::update()
{
    // nothing to do here
}

// ----------------------------------------------------------------------------------------

bool NavigateTask::wantsControl()
{
    double headingDiff = ComputeHeadingDiff( Scoreboard::navTargetHeading(),
                                             Scoreboard::imuYaw() );

    return ( std::abs( headingDiff ) > 0.0 );
}

// ----------------------------------------------------------------------------------------

void NavigateTask::takeControl()
{
    Scoreboard::sendManagerMessage( "Navigate" );

    double headingDiff = ComputeHeadingDiff( Scoreboard::navTargetHeading(),
                                             Scoreboard::imuYaw() );

    int8_t servoPosition;

    if ( headingDiff <= 12.0 && headingDiff >= -12.0 )
    {
        servoPosition = static_cast< int8_t >( headingDiff );
    }
    else if ( headingDiff < 0.0 )
    {
        servoPosition = -12;
    }
    else
    {
        servoPosition = 12;
    }

#if 0
    log().information(
        Poco::Logger::format(
            "NavigateTask: heading: $0 diff: $1 steer: $2",
            Poco::NumberFormatter::format( Scoreboard::imuYaw() ),
            Poco::NumberFormatter::format( headingDiff ),
            Poco::NumberFormatter::format( servoPosition ) ) );
#endif

    MotorsAndServos::servoPositions( -servoPosition, servoPosition );

    int8_t speed = Scoreboard::navMaximumSpeed();
    MotorsAndServos::motorSpeeds( speed, speed );
}

// ----------------------------------------------------------------------------------------