view main/robots/odr/ImuReader.h @ 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 a01a4a349d58
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/odr/ImuReader.h
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Runnable object that reads and parses messages from a SparkFun 9-DOF Razor IMU.
//
//  Copyright (c) 2012-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.
//
// ----------------------------------------------------------------------------------------

#ifndef BCDRL_ROBOTS_ODR_IMUREADER_H
#define BCDRL_ROBOTS_ODR_IMUREADER_H

#include <stdint.h>

#include <Poco/Event.h>
#include <Poco/Runnable.h>
#include <Poco/RWLock.h>
#include <Poco/Timer.h>
#include <Poco/Timestamp.h>

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

class ImuReader : public Poco::Runnable
{
    public:
        static void   imuIsOffline();
        static bool   isImuAlive();
        static double imuRoll();
        static double imuPitch();
        static double imuYaw();
        static void   imuUpdate( double roll, double pitch, double yaw );

    public:
        ImuReader( const std::string& loggerName, const std::string& serialPortPath );
        virtual ~ImuReader();
        virtual void run();
        void timeToQuit();

    private:
        typedef enum
        {
            PARSE_STATE_LOOKING_FOR_START,
            PARSE_STATE_START_SYMBOL_A,
            PARSE_STATE_START_SYMBOL_N,
            PARSE_STATE_START_SYMBOL_G,
            PARSE_STATE_START_SYMBOL_COLON,
            PARSE_STATE_READ_ROLL,
            PARSE_STATE_READ_PITCH,
            PARSE_STATE_READ_YAW

        }   imu_parse_state_t;

    private:
        static Poco::RWLock    sm_rwLock;
        static bool            sm_isImuAlive;
        static double          sm_imuRoll;
        static double          sm_imuPitch;
        static double          sm_imuYaw;
        static Poco::Timestamp sm_imuLastUpdate;

    private:
        std::string       m_loggerName;
        std::string       m_serialPortPath;
        Poco::Event       m_quitEvent;
        int               m_serialPortFd;
        imu_parse_state_t m_parseState;
        std::string       m_parseToken;
        double            m_parseValueRoll;
        double            m_parseValuePitch;
        double            m_parseValueYaw;

    private:
        void closeSerialPort();
        bool configureSerialPort();
        bool readFromSerialPort( char* buffer, size_t* length );
        void resetParseState();
        bool processImuData( const char* data, size_t length );
};

// ----------------------------------------------------------------------------------------
#endif // #ifndef BCDRL_ROBOTS_ODR_IMUREADER_H
// ----------------------------------------------------------------------------------------