view main/robots/cantool/CmdGps.cpp @ 229:79283dc7dc54 main

Add the "start" command, improve the "listen" command.
author Bob Cook <bob@bobcookdev.com>
date Sat, 12 Jul 2014 17:18:55 -0700
parents 15d96880b4ba
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/cantool/CmdGps.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Command "gps" implementation.
//
//  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 <iostream>

#include <Poco/Exception.h>
#include <Poco/NumberFormatter.h>
#include <Poco/NumberParser.h>

#include "Commands.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"
#include "packages/linux/can/CANMsgProcessor.h"

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

static void PrintHelp()
{
    std::cerr << "error: gps lat[itude]  <degrees> <minutes>" << std::endl;
    std::cerr << "error: gps lon[gitude] <degrees> <minutes>" << std::endl;
    std::cerr << "error: gps fix <satellites>" << std::endl;
    std::cerr << "       (enter negatives as: \"gps lat -- -48 17.038\")" << std::endl;
}

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

static void DoCommandLat( const std::string& degreesStr, const std::string& minutesStr )
{
    uint32_t msgid = can_build_message_id( can_node_sensor_gps,
                                           can_node_broadcast,
                                           can_dataid_gps_latitude );

    can_data_gps_data msg;

    try
    {
        int    deg = Poco::NumberParser::parse( degreesStr );
        double min = Poco::NumberParser::parseFloat( minutesStr );

        if ( deg < -180 || deg > 180 )
        {
            throw Poco::SyntaxException( "value out of range", 1 );
        }

        if ( min < 0 || min > 60.0 )
        {
            throw Poco::SyntaxException( "value out of range", 1 );
        }

        msg.degrees         = static_cast< int16_t >( deg );
        msg.min_thousandths = static_cast< uint32_t >( min * can_data_gps_min_multiplier );
    }
    catch ( const Poco::SyntaxException& ex )
    {
        PrintHelp();
        return;
    }

    CANMessage::QueueToSend(
        new CANMessage( msgid, reinterpret_cast< uint8_t* >( &msg ), sizeof( msg ) ) );

    std::cout << "sent gps latitude "
        << Poco::NumberFormatter::format( msg.degrees )
        << "," 
        << Poco::NumberFormatter::format( msg.min_thousandths ) 
        << std::endl;
}

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

static void DoCommandLon( const std::string& degreesStr, const std::string& minutesStr )
{
    uint32_t msgid = can_build_message_id( can_node_sensor_gps,
                                           can_node_broadcast,
                                           can_dataid_gps_longitude );

    can_data_gps_data msg;

    try
    {
        int    deg = Poco::NumberParser::parse( degreesStr );
        double min = Poco::NumberParser::parseFloat( minutesStr );

        if ( deg < -180 || deg > 180 )
        {
            throw Poco::SyntaxException( "value out of range", 1 );
        }

        if ( min < 0 || min > 60.0 )
        {
            throw Poco::SyntaxException( "value out of range", 1 );
        }

        msg.degrees         = static_cast< int16_t >( deg );
        msg.min_thousandths = static_cast< uint32_t >( min * can_data_gps_min_multiplier );
    }
    catch ( const Poco::SyntaxException& ex )
    {
        PrintHelp();
        return;
    }

    CANMessage::QueueToSend(
        new CANMessage( msgid, reinterpret_cast< uint8_t* >( &msg ), sizeof( msg ) ) );

    std::cout << "sent gps longitude "
        << Poco::NumberFormatter::format( msg.degrees )
        << "," 
        << Poco::NumberFormatter::format( msg.min_thousandths ) 
        << std::endl;
}

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

static void DoCommandFix( const std::string& satellitesStr )
{
    uint32_t msgid = can_build_message_id( can_node_sensor_gps,
                                           can_node_broadcast,
                                           can_dataid_gps_fix );

    can_data_gps_fix msg;

    try
    {
        int satCount = Poco::NumberParser::parse( satellitesStr );

        if ( satCount < 0 || satCount > 99 )
        {
            throw Poco::SyntaxException( "value out of range", 1 );
        }

        msg.satellites = static_cast< uint8_t >( satCount );
    }
    catch ( const Poco::SyntaxException& ex )
    {
        PrintHelp();
        return;
    }

    CANMessage::QueueToSend(
        new CANMessage( msgid, reinterpret_cast< uint8_t* >( &msg ), sizeof( msg ) ) );

    std::cout << "sent gps fix "
        << Poco::NumberFormatter::format( msg.satellites )
        << std::endl;
}

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

void CommandGps( const std::vector< std::string >& args )
{
    if ( args.size() < 3 )
    {
        PrintHelp();
        return;
    }

    if ( args[ 1 ] == "lat" || args[ 1 ] == "latitude" )
    {
        if ( args.size() != 4 )
        {
            PrintHelp();
        }
        else
        {
            DoCommandLat( args[ 2 ], args[ 3 ] );
        }
    }
    else if ( args[ 1 ] == "lon" || args[ 1 ] == "longitude" )
    {
        if ( args.size() != 4 )
        {
            PrintHelp();
        }
        else
        {
            DoCommandLon( args[ 2 ], args[ 3 ] );
        }
    }
    else if ( args[ 1 ] == "fix" )
    {
        if ( args.size() != 3 )
        {
            PrintHelp();
        }
        else
        {
            DoCommandFix( args[ 2 ] );
        }
    }
    else
    {
        PrintHelp();
    }
}

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