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

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

// ----------------------------------------------------------------------------------------
//
//  robots/odr-sim/Receiver.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Update status from incoming CAN messages.
//
//  Copyright (c) 2011-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 "Receiver.h"

#include <stdexcept>

#include <Poco/Exception.h>
#include <Poco/Thread.h>

#include <FL/Fl.H>
#include <FL/Fl_Window.H>

#include "SimDisplay.h"
#include "SonarFrontStatus.h"

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

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

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

Receiver::Receiver( SimDisplay* theDisplay )
    : Poco::Runnable(),
      m_quitEvent( false /* not auto-reset */ ),
      m_display( theDisplay )
{
}

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

Receiver::~Receiver()
{
}

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

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

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

void Receiver::recvMgrUpdate( CANMessage* msg )
{
    can_data_odrmgr_update info;
    int length = msg->msgData( reinterpret_cast< uint8_t* >( &info ), sizeof( info ) );

    if ( m_display )
    {
        m_display->doMgrHeartbeat();

        if ( length > 0 )
        {
            m_display->updateManagerMsg( info.msg, length );
        }
    }
}

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

void Receiver::recvMotorSpeed( CANMessage* msg )
{
    can_data_wheel_speed data;
    msg->msgData( reinterpret_cast< uint8_t* >( &data ), sizeof( data ) );

    if ( m_display )
    {
        m_display->updateMotorSpeeds( data.rpm_front, data.rpm_rear );
    }
}

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

void Receiver::recvServoPosition( CANMessage* msg )
{
    can_data_wheel_position data;
    msg->msgData( reinterpret_cast< uint8_t* >( &data ), sizeof( data ) );

    if ( m_display )
    {
        m_display->updateServoPositions( data.angle_front, data.angle_rear );
    }
}

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

void Receiver::recvSonarFrontStateChange( bool enabled )
{
    if ( enabled )
    {
        SonarFrontStatus::setSonarFrontEnabled();
    }
    else
    {
        SonarFrontStatus::setSonarFrontDisabled();
    }

    if ( m_display )
    {
        m_display->updateSonarFrontState( enabled );
    }
}

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

void Receiver::run()
{
    for ( ;; )
    {
        try
        {
            CANMessage* msg = CANMessage::WaitDequeueReceived( 250 ); // 250 ms

            if ( msg == 0 && m_quitEvent.tryWait( 0 ) )
            {
                return;
            }

            if ( msg == 0 )
            {
                continue;
            }

            uint8_t  srcNode;
            uint8_t  dstNode;
            uint16_t dataId;

            can_parse_message_id( 
                    msg->msgIdentifier(), &srcNode, &dstNode, &dataId );

            switch ( dataId )
            {
                case can_dataid_odrmgr_update:
                    recvMgrUpdate( msg );
                    break;

                case can_dataid_wheel_speed:
                    recvMotorSpeed( msg );
                    break;

                case can_dataid_wheel_position:
                    recvServoPosition( msg );
                    break;

                case can_dataid_sonar_front_enable:
                    recvSonarFrontStateChange( true );
                    break;

                case can_dataid_sonar_front_disable:
                    recvSonarFrontStateChange( false );
                    break;
            }
        }
        catch ( ... )
        {
            //log.error( "Receiver::run() caught unknown exception" );
        }

        // sleep for 1/2 second after exception processing, but don't exit
        Poco::Thread::sleep( 500 );
    }
}

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