changeset 192:153436c2a354 main

These should have been added long ago...
author Bob Cook <bob@bobcookdev.com>
date Sun, 30 Jun 2013 10:54:30 -0700
parents e23198d929e9
children ff6cfa2ea1ee
files main/robots/odr/StartupTask.cpp main/robots/odr/StartupTask.h
diffstat 2 files changed, 217 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/odr/StartupTask.cpp	Sun Jun 30 10:54:30 2013 -0700
@@ -0,0 +1,158 @@
+// ----------------------------------------------------------------------------------------
+//
+//  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 )
+    {
+        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 );
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/odr/StartupTask.h	Sun Jun 30 10:54:30 2013 -0700
@@ -0,0 +1,59 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/odr/StartupTask.h
+//    
+//  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.
+//
+// ----------------------------------------------------------------------------------------
+
+#ifndef BCDRL_ROBOTS_ODR_STARTUPTASK_H
+#define BCDRL_ROBOTS_ODR_STARTUPTASK_H
+
+#include "TaskObject.h"
+
+// ----------------------------------------------------------------------------------------
+
+class StartupTask : public TaskObject
+{
+    public:
+        StartupTask( const std::string& loggerName );
+        virtual ~StartupTask();
+        virtual void update();
+        virtual bool wantsControl();
+        virtual void takeControl();
+
+    private:
+        bool            m_lastControllerStatus;
+        Poco::Timestamp m_lastMessageTime;
+        bool            m_gotButtonPress;
+        bool            m_gotButtonRelease;
+        Poco::Timestamp m_startRunTime;
+};
+
+// ----------------------------------------------------------------------------------------
+#endif // #ifndef BCDRL_ROBOTS_ODR_STARTUPTASK_H
+// ----------------------------------------------------------------------------------------
+