Tatsuya Yamamoto / Mbed 2 deprecated UV_Robot_Nucleo

Dependencies:   mbed X_NUCLEO_PLC01A1 ros_lib_melodic

uv_robot.cpp

Committer:
yamadola
Date:
2020-08-15
Revision:
1:ef4b86795d79
Parent:
0:43eb9ccc1583
Child:
2:72761505a690

File content as of revision 1:ef4b86795d79:

#include "uv_robot.h"

UVRobot::UVRobot():_can(),_plc(),_rs485(),
_wheel(&_can, &_plc), _topLamp(2, 0x01, &_plc, &_rs485), _bottomLamp(3, &_plc), _footLamp(4, &_plc),
_enable(enablePin, &_plc), _error(errorSigPin, &_plc), _emgSW(emgSigPin, &_plc){}

enum state UVRobot::getState(){
    return _robotState;
}

void UVRobot::setState( enum state robotState ){
    _robotState = robotState;
}

void UVRobot::init(){
    setState( Init );
    _enable = 1;
    _error = 0;
    _wheel.init();
    _topLamp.init();
    _bottomLamp.init();
    _footLamp.init();
    setState( Idle );
}

void UVRobot::start(){
    _robotState = Run;
    std_msgs::Bool  sMsg;
    std_msgs::Float32MultiArray  rMsg;
    /* 
    * rMsg.data[0] : Right Wheel RPM
    * rMsg.data[1] : Left Wheel RPM
    * rMsg.data[2] : Top Lamp On/Off (On:1 Off:0)
    * rMsg.data[3] : Top Lamp Angle
    * rMsg.data[4] : Bottom Lamp On/Off (On:1 Off:0)
    * rMsg.data[5] : Foot Lamp On/Off (On:1 Off:0)
    * rMsg.data[6] : Error Signal (Error:1 Nominal:0)
    */
    
    while(1){
        sMsg.data = _emgSW;
        rMsg = _RosI.readMsg();
        
        _wheel.setVelocity( rMsg.data[0], rMsg.data[1] );
        _topLamp.setUVPower( (uint8_t)rMsg.data[2] );
        _topLamp.setLightAngle( rMsg.data[3] );
        _bottomLamp.setUVPower( (uint8_t)rMsg.data[4] );
        _footLamp.setUVPower( (uint8_t)rMsg.data[5] );
        
        _wheel.drive();
        _topLamp.drive();
        
        _RosI.sendMsg(sMsg);
        _RosI._nh.spinOnce();
        wait_ms(5);
        
        if( _emgSW || (bool)rMsg.data[6] ){
            UVRobot::error();
        }
    }
}

void UVRobot::stop(){
    _wheel.stop();
    _topLamp.stop();
    _bottomLamp.setUVPower(0);
    _footLamp.setUVPower(0);
    _robotState = Idle;
}

void UVRobot::error(){
    _wheel.brake();
    _topLamp.stop();
    _bottomLamp.setUVPower(0);
    _footLamp.setUVPower(0);
    _robotState = Error;
}