view main/robots/cantool/CanToolApp.cpp @ 276:3b91c5d76887 main

Add the pixy command
author Bob Cook <bob@bobcookdev.com>
date Mon, 30 May 2016 19:01:37 -0700
parents 79283dc7dc54
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/cantool/CanToolApp.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Application object implementation for demonstrating the CANSocket class.    
//
//  Copyright (c) 2011-2016 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 <iostream>
#include <memory>
#include <stdio.h>
#include <string>
#include <vector>

#include <Poco/AutoPtr.h>
#include <Poco/Thread.h>
#include <Poco/Util/Application.h>
#include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/Option.h>
#include <Poco/Util/OptionSet.h>

#include "CanToolApp.h"
#include "Commands.h"

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

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

CanToolApp::CanToolApp()
    : m_helpRequested( false ),
      m_canInterfaceName( "can0" ),
      m_commandDispatchTable()
{
}

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

CanToolApp::~CanToolApp()
{
}

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

void CanToolApp::initialize( Poco::Util::Application& self )
{
    loadConfiguration();
    Poco::Util::Application::initialize( self );

    m_commandDispatchTable[ "estop" ] = CommandEStop;
    m_commandDispatchTable[ "gps" ] = CommandGps;
    m_commandDispatchTable[ "listen" ] = CommandListen;
    m_commandDispatchTable[ "pixy" ] = CommandPixy;
    m_commandDispatchTable[ "sonarfront" ] = CommandSonarFront;
    m_commandDispatchTable[ "speed" ] = CommandSpeed;
    m_commandDispatchTable[ "start" ] = CommandStartPgm;
    m_commandDispatchTable[ "steer" ] = CommandSteer;
}

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

void CanToolApp::uninitialize()
{
    Poco::Util::Application::uninitialize();
}

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

void CanToolApp::defineOptions( Poco::Util::OptionSet& options )
{
    Poco::Util::Application::defineOptions( options );

    options.addOption(
        Poco::Util::Option( "help", "h", "display argument help information" )
            .required( false )
            .repeatable( false )
            .callback( Poco::Util::OptionCallback< CanToolApp >(
                                                    this, &CanToolApp::handleHelp ) ) );

    options.addOption(
        Poco::Util::Option( "interface", "i", "specify the CAN interface" )
            .required( false )
            .repeatable( false )
            .callback( Poco::Util::OptionCallback< CanToolApp >(
                                               this, &CanToolApp::setCanInterface ) ) );
}

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

void CanToolApp::handleHelp( const std::string& name, const std::string& value )
{
    Poco::Util::HelpFormatter helpFormatter( options() );
    helpFormatter.setCommand( commandName() );
    helpFormatter.format( std::cout );
    stopOptionsProcessing();
    m_helpRequested = true;
}

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

void CanToolApp::setCanInterface( const std::string& name, const std::string& value )
{
    m_canInterfaceName = value;
}

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

int CanToolApp::main( const std::vector< std::string >& args )
{
    if ( m_helpRequested )
    {
        return Poco::Util::Application::EXIT_OK;
    }

    loadConfiguration();

    Poco::Thread    canMsgProcessorThread;
    CANMsgProcessor canMsgProcessor( m_canInterfaceName );
    canMsgProcessorThread.start( canMsgProcessor );

    if ( ! args.empty() )
    {
        if ( m_commandDispatchTable.find( args[ 0 ] ) != m_commandDispatchTable.end() )
        {
            ( m_commandDispatchTable[ args[ 0 ] ] )( args );
        }
        else
        {
            std::cerr << "command \"" << args[ 0 ] << "\" not recognized" << std::endl;
        }
    }
    else
    {
        std::cerr << "error: no command given" << std::endl;
    }

    canMsgProcessor.timeToQuit();
    canMsgProcessorThread.join();

    return Poco::Util::Application::EXIT_OK;
}

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