view main/robots/odr-sim/ImuStatus.cpp @ 224:5e91f5877f1f main

Latest simulator changes.
author Bob Cook <bob@bobcookdev.com>
date Thu, 17 Apr 2014 22:03:28 -0700
parents
children 783f69f37c64
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/odr-sim/ImuStatus.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Thread that periodically sends the IMU details.
//
//  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 "ImuStatus.h"

#include "SimDisplay.h"

#include "packages/common/can/can_helpers.h"
#include "packages/common/can/can_messages.h"
#include "packages/common/can/can_nodes.h"

#include "packages/linux/can/CANMessage.h"

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

static void SendImuDataMessages()
{
#if 0
    uint32_t msgid;

    if ( SonarFrontStatus::isSonarFrontEnabled() )
    {
        msgid = can_build_message_id( can_node_odr_sonar_front,
                                      can_node_broadcast,
                                      can_dataid_sonar_front_state_enabled );
    }
    else
    {
        msgid = can_build_message_id( can_node_odr_sonar_front,
                                      can_node_broadcast,
                                      can_dataid_sonar_front_state_disabled );
    }
    CANMessage::QueueToSend( new CANMessage( msgid ) );
#endif
}

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

Poco::RWLock ImuStatus::sm_rwLock;
bool         ImuStatus::sm_isEnabled = false;
double       ImuStatus::sm_yaw       = 0.0;
double       ImuStatus::sm_pitch     = 0.0;
double       ImuStatus::sm_roll      = 0.0;

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

bool ImuStatus::isImuEnabled()
{
    Poco::RWLock::ScopedReadLock lock( sm_rwLock );
    return sm_isEnabled;
}

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

void ImuStatus::setImuEnabled()
{
    Poco::RWLock::ScopedWriteLock lock( sm_rwLock );
    sm_isEnabled = true;
}

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

void ImuStatus::setImuDisabled()
{
    Poco::RWLock::ScopedWriteLock lock( sm_rwLock );
    sm_isEnabled = false;
}

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

double ImuStatus::imuYaw()
{
    Poco::RWLock::ScopedReadLock lock( sm_rwLock );
    return sm_yaw;
}

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

double ImuStatus::imuPitch()
{
    Poco::RWLock::ScopedReadLock lock( sm_rwLock );
    return sm_pitch;
}

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

double ImuStatus::imuRoll()
{
    Poco::RWLock::ScopedReadLock lock( sm_rwLock );
    return sm_roll;
}

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

void ImuStatus::setImuYaw( double value )
{
    Poco::RWLock::ScopedWriteLock lock( sm_rwLock );
    sm_yaw = value;
}

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

void ImuStatus::setImuPitch( double value )
{
    Poco::RWLock::ScopedWriteLock lock( sm_rwLock );
    sm_pitch = value;
}

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

void ImuStatus::setImuRoll( double value )
{
    Poco::RWLock::ScopedWriteLock lock( sm_rwLock );
    sm_roll = value;
}

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

ImuStatus::ImuStatus()
    : Poco::Runnable(),
      m_quitEvent( true /* auto-reset */ )
{
}

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

ImuStatus::~ImuStatus()
{
}

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

void ImuStatus::timeToQuit()
{
    m_quitEvent.set();
}

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

void ImuStatus::run()
{
    for ( ;; )
    {
        try
        {
//                SendSonarFrontStatusMessage();

//            if ( isSonarFrontEnabled() )
//            {
//                SendSonarFrontValueMessage();
//            }
        }
        catch ( ... )
        {
            // nothing to do, but don't stop the thread
        }

        if ( m_quitEvent.tryWait( 500 ) ) // 500ms = 1/2 second
        {
            return;
        }
    }
}

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