view main/robots/odr/StartupTask.cpp @ 225:c4009187a522 main

Checkpoint
author Bob Cook <bob@bobcookdev.com>
date Sat, 28 Jun 2014 16:12:28 -0700
parents 153436c2a354
children 50414c680910
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/odr/StartupTask.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Subsumption task to enact safety measures under certain unsafe conditions.
//
//  Copyright (c) 2011 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 "StartupTask.h"

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

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

const Poco::Timestamp::TimeDiff  TimeToRun = Poco::Timestamp::resolution() * 300;

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

StartupTask::StartupTask( const std::string& loggerName )
    : TaskObject( loggerName ),
      m_lastControllerStatus( Scoreboard::isControllerAlive() ),
      m_lastMessageTime(),
      m_gotButtonPress( false ),
      m_gotButtonRelease( false ),
      m_startRunTime()
{
}

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

StartupTask::~StartupTask()
{
}

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

void StartupTask::update()
{
    // we need the odr-controller to be running
    
    if ( ! Scoreboard::isControllerAlive() )
    {
        if ( m_lastControllerStatus )
        {
            log().information( "StartupTask: controller is not alive" );
            m_lastControllerStatus = false;
        }

        return; // nothing else can be done
    }
    else if ( ! m_lastControllerStatus )
    {
        log().information( "StartupTask: controller is alive" );
        m_lastControllerStatus = true;
    }

    // check for the button press then release event

    if ( ! m_gotButtonPress )
    {
        m_gotButtonPress = Scoreboard::isButtonOneDown();
        if ( m_gotButtonPress ) log().information( "StartupTask: got button press" );
    }

    if ( m_gotButtonPress && ! m_gotButtonRelease )
    {
        m_gotButtonRelease = ! Scoreboard::isButtonOneDown();
        if ( m_gotButtonRelease ) log().information( "StartupTask: got button release" );

        if ( m_gotButtonRelease )
        {
            log().information( "StartupTask: starting run" );
            m_startRunTime.update();

//            Scoreboard::navSetTargetHeading( Scoreboard::imuYaw() );
        }
    }

    // if we are running, check for timeout

    if ( m_gotButtonPress && m_gotButtonRelease )
    {
        if ( m_startRunTime.isElapsed( TimeToRun ) )
        {
            log().information( "StartupTask: ending run" );
            m_gotButtonPress   = false;
            m_gotButtonRelease = false;
        }
    }
}

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

bool StartupTask::wantsControl()
{
    if ( ! m_lastControllerStatus )
    {
        return true; // take control if the odr-controller is not yet alive
    }

    if ( ! m_gotButtonPress || ! m_gotButtonRelease )
    {
        return true; // take control if the button hasn't been pressed/released yet
    }

    return false; // otherwise not for us
}

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

void StartupTask::takeControl()
{
    if ( ! m_lastControllerStatus )
    {
        Scoreboard::sendManagerMessage( "Waiting" );
        return; // nothing can be done until the odr-controller is up
    }

    // send instructions for display on the odr-controller

    if ( m_lastMessageTime.isElapsed( TaskObject::TwoSeconds ) )
    {
        Scoreboard::sendManagerMessage( "Press A" );
        m_lastMessageTime.update();
    }

    // make sure we aren't moving

    MotorsAndServos::servoPositions( 0, 0 );
    MotorsAndServos::motorSpeeds( 0, 0 );
}

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