view main/robots/odr/SafetyTask.cpp @ 230:50414c680910 main

Split the IMU reader out to a separate application. Rework the scoreboard accordingly. Also remove all the legacy "odr-controller" logic, and improve the management of "emergency" situations for all nodes.
author Bob Cook <bob@bobcookdev.com>
date Sat, 12 Jul 2014 17:21:10 -0700
parents caee904383e5
children
line wrap: on
line source

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

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

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

SafetyTask::SafetyTask( const std::string& loggerName )
    : TaskObject( loggerName ),
      m_lastMessageTime(),
      m_lastEmergencyStatus( Scoreboard::isEmergencyActive() ),
      m_lastDisplayStatus( Scoreboard::isDisplayAlive() ),
      m_lastGpsStatus( Scoreboard::isGpsAlive() ),
      m_lastImuStatus( Scoreboard::isImuAlive() ),
      m_lastMotionStatus( Scoreboard::isMotionAlive() ),
      m_lastSonarFrontStatus( Scoreboard::isSonarFrontAlive() )
{
}

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

SafetyTask::~SafetyTask()
{
}

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

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

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

bool SafetyTask::wantsControl()
{
    // overall emergency situation
    
    if ( Scoreboard::isEmergencyActive() )
    {
        if ( ! m_lastEmergencyStatus )
        {
            log().information( "SafetyTask: emergency situation active" );
            m_lastEmergencyStatus = true;
        }

        return true;
    }
    else if ( m_lastEmergencyStatus )
    {
        log().information( "SafetyTask: emergency situation cleared" );
        m_lastEmergencyStatus = false;
    }

    // odr-display
    
    if ( ! Scoreboard::isDisplayAlive() )
    {
        if ( m_lastDisplayStatus )
        {
            log().information( "SafetyTask: odr-display is not alive" );
            m_lastDisplayStatus = false;
        }

        return true;
    }
    else if ( ! m_lastDisplayStatus )
    {
        log().information( "SafetyTask: odr-display is alive" );
        m_lastDisplayStatus = true;
    }

    // odr-gps
    
    if ( ! Scoreboard::isGpsAlive() )
    {
        if ( m_lastGpsStatus )
        {
            log().information( "SafetyTask: sensor-gps is not alive" );
            m_lastGpsStatus = false;
        }

        return true;
    }
    else if ( ! m_lastGpsStatus )
    {
        log().information( "SafetyTask: sensor-gps is alive" );
        m_lastGpsStatus = true;
    }

    // odr-motion
    
    if ( ! Scoreboard::isMotionAlive() )
    {
        if ( m_lastMotionStatus )
        {
            log().information( "SafetyTask: odr-motion is not alive" );
            m_lastMotionStatus = false;
        }

        return true;
    }
    else if ( ! m_lastMotionStatus )
    {
        log().information( "SafetyTask: odr-motion is alive" );
        m_lastMotionStatus = true;
    }

    // odr-sonar-front

    if ( ! Scoreboard::isSonarFrontAlive() )
    {
        if ( m_lastSonarFrontStatus )
        {
            log().information( "SafetyTask: front sonar is not alive" );
            m_lastSonarFrontStatus = false;
        }

        return true;
    }
    else if ( ! m_lastSonarFrontStatus )
    {
        log().information( "SafetyTask: front sonar is alive" );
        m_lastSonarFrontStatus = true;
    }

    // imu
    
    if ( ! Scoreboard::isImuAlive() )
    {
        if ( m_lastImuStatus )
        {
            log().information( "SafetyTask: sensor-imu is not alive" );
            m_lastImuStatus = false;
        }

        return true;
    }
    else if ( ! m_lastImuStatus )
    {
        log().information( "SafetyTask: sensor-imu is alive" );
        m_lastImuStatus = true;
    }

    // otherwise no issues

    return false;
}

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

void SafetyTask::takeControl()
{
    if ( m_lastMessageTime.isElapsed( TaskObject::TwoSeconds ) )
    {
        Scoreboard::sendManagerMessage( "Unsafe!" );
        m_lastMessageTime.update();
    }

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

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