view main/robots/odr-sonar-front/canmsgs.cpp @ 220:74381e5dd204 main

Demo for the Adafruit LCD product number 1480.
author Bob Cook <bob@bobcookdev.com>
date Mon, 07 Jul 2014 18:42:15 -0700
parents 7411b6a1dba2
children b568dde5f512
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  Copyright (C) 2011-2014 Bob Cook
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  CAN related functions for the Outdoor Robot sensor node.
//
//  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 "project_defs.h"

#include "func.h"

#include "packages/avr/can/m1.h"

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

#include "packages/avr/device/spinwait.h"

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

bool canmsg_init()
{
    return m1can_init();
}

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

bool canmsg_did_error_occur()
{
    return ( m1can_status() != m1can_status_ok );
}

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

bool canmsg_send_emergency()
{
    return m1can_send( can_build_message_id( can_node_odr_sonar_front,
                                             can_node_broadcast,
                                             can_dataid_emergency ), 0, 0 );
}

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

bool canmsg_send_all_clear()
{
    return m1can_send( can_build_message_id( can_node_odr_sonar_front,
                                             can_node_broadcast,
                                             can_dataid_all_clear ), 0, 0 );
}

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

bool canmsg_send_status( bool enabled )
{
    uint32_t msgid;

    if ( enabled )
    {
        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 );
    }

    return m1can_send( msgid, 0, 0 );
}

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

bool canmsg_send_sonar_front
(
    uint8_t left,
    uint8_t center_left,
    uint8_t center_right,
    uint8_t right
)
{
    uint32_t msgid = can_build_message_id( can_node_odr_sonar_front,
                                           can_node_broadcast,
                                           can_dataid_sonar_front );

    can_data_sonar_front data;

    data.left         = left;
    data.center_left  = center_left;
    data.center_right = center_right;
    data.right        = right;

    return m1can_send( msgid, reinterpret_cast< uint8_t* >( &data ), sizeof( data ) );
}

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

bool canmsg_process_pending()
{
    uint32_t recvid;
    bool     recvrequest = false;
    uint8_t  recvdata[ 8 ];
    uint8_t  recvlen = sizeof( recvdata );

    for ( ;; )
    {
        if ( ! m1can_read( &recvid, &recvrequest, recvdata, &recvlen ) )
        {
            return true; // no messages, everything ok
        }

        uint8_t  srcnode;
        uint8_t  dstnode;
        uint16_t dataid;
        can_parse_message_id( recvid, &srcnode, &dstnode, &dataid );

        if ( dstnode != can_node_broadcast && dstnode != can_node_odr_sonar_front )
        {
            return true; // not for us, everything ok
        }

        switch ( dataid )
        {
                case can_dataid_heartbeat:
                    status_got_heartbeat( srcnode );
                    break;

                case can_dataid_emergency:
                    status_got_emergency( srcnode );
                    break;

                case can_dataid_all_clear:
                    status_got_all_clear( srcnode );
                    break;

            case can_dataid_sonar_front_enable:
                sonar_start();
                break;

            case can_dataid_sonar_front_disable:
                sonar_stop();
                break;
        }
    }

    return true; // no errors
}

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