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

Dependencies:   mbed

Committer:
efLPdib4455
Date:
Sun Sep 06 05:53:03 2020 +0000
Revision:
0:f25ac5f8c95e
Child:
1:f06777cfcc8d
mbed_os_2(v.171) r020906

Who changed what in which revision?

UserRevisionLine numberNew contents of line
efLPdib4455 0:f25ac5f8c95e 1 // mbed-os rev.171(mbed_os=2, very old) (r020904,sige)
efLPdib4455 0:f25ac5f8c95e 2 // (Ticker, wait) used.
efLPdib4455 0:f25ac5f8c95e 3 // 1--CANtest-mbed_os_2-01_02
efLPdib4455 0:f25ac5f8c95e 4 // cp + mod CAN loopback example of the official doc (r020811,sige)
efLPdib4455 0:f25ac5f8c95e 5 // std data frame of (position command of the HT02 motor) is dispatched
efLPdib4455 0:f25ac5f8c95e 6 // thru can1, which is then received by can2.
efLPdib4455 0:f25ac5f8c95e 7 // (can1,can2) seems half duplex.
efLPdib4455 0:f25ac5f8c95e 8 //
efLPdib4455 0:f25ac5f8c95e 9 // (the actual HT02 motor would send back the response data frame?)
efLPdib4455 0:f25ac5f8c95e 10
efLPdib4455 0:f25ac5f8c95e 11 //#if !DEVICE_CAN
efLPdib4455 0:f25ac5f8c95e 12 //#error [NOT_SUPPORTED] CAN not supported for this target
efLPdib4455 0:f25ac5f8c95e 13 //#endif
efLPdib4455 0:f25ac5f8c95e 14
efLPdib4455 0:f25ac5f8c95e 15 #include "mbed.h"
efLPdib4455 0:f25ac5f8c95e 16 #include "CAN.h"
efLPdib4455 0:f25ac5f8c95e 17 // CAN.h is required, or ERR occurred..
efLPdib4455 0:f25ac5f8c95e 18 // #include "rtos.h"
efLPdib4455 0:f25ac5f8c95e 19 // where is rtos?
efLPdib4455 0:f25ac5f8c95e 20
efLPdib4455 0:f25ac5f8c95e 21 Ticker ticker;
efLPdib4455 0:f25ac5f8c95e 22 DigitalOut led1(LED1);
efLPdib4455 0:f25ac5f8c95e 23 DigitalOut led2(LED2);
efLPdib4455 0:f25ac5f8c95e 24
efLPdib4455 0:f25ac5f8c95e 25 // MBED_CONF_APP_CAN1_RD undefined,
efLPdib4455 0:f25ac5f8c95e 26 // thus explicitly defines (RX, Tx) pins instead.
efLPdib4455 0:f25ac5f8c95e 27 // The constructor takes in RX, and TX pin respectively.
efLPdib4455 0:f25ac5f8c95e 28 // These pins, for this example, are defined in mbed_app.json
efLPdib4455 0:f25ac5f8c95e 29 // CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
efLPdib4455 0:f25ac5f8c95e 30 // CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
efLPdib4455 0:f25ac5f8c95e 31
efLPdib4455 0:f25ac5f8c95e 32 CAN can1(p30, p29); // (RX,TX)pins in this order
efLPdib4455 0:f25ac5f8c95e 33 CAN can2( p9, p10);
efLPdib4455 0:f25ac5f8c95e 34
efLPdib4455 0:f25ac5f8c95e 35 char c_sent=0, c_read=0, cloop=0 ; // counters of (sent, read, loop)
efLPdib4455 0:f25ac5f8c95e 36 char dseg[8]={0xFF, 0x01, 0x00, 0x64, 0x00, 0x80, 0x00, 0x00};
efLPdib4455 0:f25ac5f8c95e 37 //
efLPdib4455 0:f25ac5f8c95e 38 // data segment specific to the HT02 motor
efLPdib4455 0:f25ac5f8c95e 39 // [0]=host_addr(0xff=this_MCU, 0x01=motor_#01),
efLPdib4455 0:f25ac5f8c95e 40 // [1]=func_code(01=pos.cmd), [2]=[4]=reserved(?)
efLPdib4455 0:f25ac5f8c95e 41 // [3]=acce=*256[rpm/s], [5]=target velo.[rpm] (of the motor axis, maybe)
efLPdib4455 0:f25ac5f8c95e 42 // [6]=pos.lower byte, [7]=pos.higher byte (2^16[ppc]=65536)
efLPdib4455 0:f25ac5f8c95e 43 //
efLPdib4455 0:f25ac5f8c95e 44 // 0xFFFF=0x10000-1=16^4-1=65535, 0xFF00=0xFF *16^2=0xFF *256
efLPdib4455 0:f25ac5f8c95e 45 // 0x0100=0x01 *16^2[p]= 360/16^4[deg/p] *16^2[p]=360/16^2[deg]=1.40625[deg]
efLPdib4455 0:f25ac5f8c95e 46 // with the reduction ratio R=6, the output axis=360/16^2/6=0.234375[deg]
efLPdib4455 0:f25ac5f8c95e 47 //
efLPdib4455 0:f25ac5f8c95e 48 void send()
efLPdib4455 0:f25ac5f8c95e 49 {
efLPdib4455 0:f25ac5f8c95e 50 printf("+sending can1... \n\r");
efLPdib4455 0:f25ac5f8c95e 51 if( can1.write(CANMessage(1, dseg, 8, CANData, CANStandard)) ) {
efLPdib4455 0:f25ac5f8c95e 52 c_sent++;
efLPdib4455 0:f25ac5f8c95e 53 dseg[7]+=1;
efLPdib4455 0:f25ac5f8c95e 54 printf(" ++sent can1 c_sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]); }
efLPdib4455 0:f25ac5f8c95e 55 else
efLPdib4455 0:f25ac5f8c95e 56 printf(" ++failed can1.write c_sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
efLPdib4455 0:f25ac5f8c95e 57
efLPdib4455 0:f25ac5f8c95e 58 led1 = !led1;
efLPdib4455 0:f25ac5f8c95e 59 } // endof send
efLPdib4455 0:f25ac5f8c95e 60
efLPdib4455 0:f25ac5f8c95e 61 int main()
efLPdib4455 0:f25ac5f8c95e 62 {
efLPdib4455 0:f25ac5f8c95e 63 printf("+entered main. attached send to the ticker (of mbed_os_2) every 1s. \n\r");
efLPdib4455 0:f25ac5f8c95e 64 can1.frequency(1000000); can2.frequency(1000000); // CANbus=1Mbps
efLPdib4455 0:f25ac5f8c95e 65 ticker.attach(&send, 1);
efLPdib4455 0:f25ac5f8c95e 66 CANMessage msg; // remote msg? what determines the size(msg)?
efLPdib4455 0:f25ac5f8c95e 67 while(1){
efLPdib4455 0:f25ac5f8c95e 68 cloop++;
efLPdib4455 0:f25ac5f8c95e 69 printf("+inwhile. cloop=%d ..... \n\r", cloop);
efLPdib4455 0:f25ac5f8c95e 70 if (can2.read(msg)) { // org=can2
efLPdib4455 0:f25ac5f8c95e 71 c_read++;
efLPdib4455 0:f25ac5f8c95e 72 printf(" ++fetched can2. c_read=%d, d[7]_read=%d\n\r", c_read, msg.data[7]);
efLPdib4455 0:f25ac5f8c95e 73 led2 = !led2;
efLPdib4455 0:f25ac5f8c95e 74 }
efLPdib4455 0:f25ac5f8c95e 75 else
efLPdib4455 0:f25ac5f8c95e 76 printf(" ++failed can2.read... sent=%d\n\r", c_sent);
efLPdib4455 0:f25ac5f8c95e 77 wait(0.2);
efLPdib4455 0:f25ac5f8c95e 78 // ThisThread::sleep_for(200); // in [ms], maybe.. ThisThread in rtos?
efLPdib4455 0:f25ac5f8c95e 79 } // endof inf_while
efLPdib4455 0:f25ac5f8c95e 80 } // endof main