Rihito Ohata / Mbed 2 deprecated 1-CAN-th2mot-mbed2-01_02

Dependencies:   mbed

main.cpp

Committer:
efLPdib4455
Date:
2020-09-06
Revision:
0:f25ac5f8c95e
Child:
1:f06777cfcc8d

File content as of revision 0:f25ac5f8c95e:

// mbed-os rev.171(mbed_os=2, very old)  (r020904,sige)
// (Ticker, wait) used.
// 1--CANtest-mbed_os_2-01_02  
//   cp + mod CAN loopback example of the official doc (r020811,sige)
//   std data frame of (position command of the HT02 motor) is dispatched
//   thru can1, which is then received by can2.
//   (can1,can2) seems half duplex.  
//
//   (the actual HT02 motor would send back the response data frame?)

//#if !DEVICE_CAN
//#error [NOT_SUPPORTED] CAN not supported for this target
//#endif

#include "mbed.h"
#include "CAN.h"
// CAN.h is required, or ERR occurred..
// #include "rtos.h"
// where is rtos?

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);

//  MBED_CONF_APP_CAN1_RD undefined,
//  thus explicitly defines (RX, Tx) pins instead.
//    The constructor takes in RX, and TX pin respectively.
//    These pins, for this example, are defined in mbed_app.json
//    CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
//    CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);

CAN can1(p30, p29);     // (RX,TX)pins in this order
CAN can2( p9, p10);

char c_sent=0, c_read=0, cloop=0 ;   // counters of (sent, read, loop)
char dseg[8]={0xFF, 0x01, 0x00, 0x64, 0x00, 0x80, 0x00, 0x00};
//
// data segment specific to the HT02 motor
//  [0]=host_addr(0xff=this_MCU, 0x01=motor_#01),
//  [1]=func_code(01=pos.cmd), [2]=[4]=reserved(?)
//  [3]=acce=*256[rpm/s], [5]=target velo.[rpm] (of the motor axis, maybe)
//  [6]=pos.lower byte,      [7]=pos.higher byte (2^16[ppc]=65536)
//
// 0xFFFF=0x10000-1=16^4-1=65535, 0xFF00=0xFF *16^2=0xFF *256
// 0x0100=0x01 *16^2[p]= 360/16^4[deg/p] *16^2[p]=360/16^2[deg]=1.40625[deg]
// with the reduction ratio R=6, the output axis=360/16^2/6=0.234375[deg]
//
void send()
{
    printf("+sending can1... \n\r");
    if( can1.write(CANMessage(1, dseg, 8, CANData, CANStandard)) ) {
        c_sent++;
        dseg[7]+=1;
        printf(" ++sent can1 c_sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);  }
    else
        printf(" ++failed can1.write c_sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
 
    led1 = !led1;
}   //  endof send       

int main()
{
    printf("+entered main. attached send to the ticker (of mbed_os_2) every 1s. \n\r");
    can1.frequency(1000000);    can2.frequency(1000000);  // CANbus=1Mbps
    ticker.attach(&send, 1);
    CANMessage msg;      // remote msg? what determines the size(msg)?
    while(1){
        cloop++;
        printf("+inwhile. cloop=%d ..... \n\r", cloop);
        if (can2.read(msg)) {   // org=can2
            c_read++;
            printf(" ++fetched can2. c_read=%d, d[7]_read=%d\n\r", c_read, msg.data[7]);
            led2 = !led2;
        }
        else
            printf(" ++failed can2.read...  sent=%d\n\r", c_sent);
        wait(0.2);
//      ThisThread::sleep_for(200);   // in [ms], maybe.. ThisThread in rtos?
    }   //  endof inf_while
}       //  endof main