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

Dependencies:   mbed

Committer:
efLPdib4455
Date:
Thu Sep 17 07:09:17 2020 +0000
Revision:
3:43a6f6157fcf
Parent:
2:d28f9138ca4b
Child:
4:6d957989b372
theta to motor HT02(GIM8008), published(unlisted) anyway, r020917

Who changed what in which revision?

UserRevisionLine numberNew contents of line
efLPdib4455 2:d28f9138ca4b 1 //
efLPdib4455 2:d28f9138ca4b 2 // mbed-os rev.171(mbed2, very old, deprecated BUT useful) (r020904,sige)
efLPdib4455 1:f06777cfcc8d 3 // 1--CAN-th2mot-mbed2-01_01
efLPdib4455 1:f06777cfcc8d 4 // packs the position(0-36000) command to the data_segment and
efLPdib4455 1:f06777cfcc8d 5 // dispatches the std_data_frame to the HT02(=GI8008) motor thru CAN1.
efLPdib4455 1:f06777cfcc8d 6 // returns the received data_seg from the motor.
efLPdib4455 1:f06777cfcc8d 7 // (the actual HT02 motor would send back the response data frame?)
efLPdib4455 0:f25ac5f8c95e 8 //
efLPdib4455 1:f06777cfcc8d 9 // th2mot(double th, unsigned int id) (th=theta_in, id=CANid_in)
efLPdib4455 1:f06777cfcc8d 10 //
efLPdib4455 2:d28f9138ca4b 11 // data segment specific to the HT02 motor
efLPdib4455 2:d28f9138ca4b 12 // [0]=host_addr(0xff=this_MCU, 0x01=motor_#01),
efLPdib4455 2:d28f9138ca4b 13 // [1]=func_code(01=pos.cmd), [2]=[4]=reserved(?)
efLPdib4455 2:d28f9138ca4b 14 // [3]=acce=*256[rpm/s], [5]=target velo.[rpm] (of motor axis wo the gear, maybe)
efLPdib4455 2:d28f9138ca4b 15 // [6]=pos.lower byte, [7]=pos.higher byte (of output axis, maybe)[0-36000]
efLPdib4455 0:f25ac5f8c95e 16
efLPdib4455 2:d28f9138ca4b 17 #include "mbed.h"
efLPdib4455 2:d28f9138ca4b 18 #include "CAN.h"
efLPdib4455 0:f25ac5f8c95e 19
efLPdib4455 1:f06777cfcc8d 20 // Ticker ticker;
efLPdib4455 0:f25ac5f8c95e 21 DigitalOut led1(LED1);
efLPdib4455 0:f25ac5f8c95e 22 DigitalOut led2(LED2);
efLPdib4455 2:d28f9138ca4b 23 Serial pc(USBTX, USBRX);
efLPdib4455 0:f25ac5f8c95e 24
efLPdib4455 0:f25ac5f8c95e 25 CAN can1(p30, p29); // (RX,TX)pins in this order
efLPdib4455 0:f25ac5f8c95e 26 CAN can2( p9, p10);
efLPdib4455 0:f25ac5f8c95e 27
efLPdib4455 1:f06777cfcc8d 28 char c_sent=0, c_read=0; // counters of sent and read
efLPdib4455 1:f06777cfcc8d 29 char dseg[8]={0xFF, 0x01, 0x00, 0x64, 0x00, 0x80, 0x00, 0x00}; //data_segment
efLPdib4455 1:f06777cfcc8d 30
efLPdib4455 2:d28f9138ca4b 31 // byte manipu using the union Hoge, char MUST be unsigned.
efLPdib4455 1:f06777cfcc8d 32 union Hoge{ unsigned char c[2]; uint16_t ii; };
efLPdib4455 1:f06777cfcc8d 33
efLPdib4455 1:f06777cfcc8d 34 void th2mot(double th, unsigned int id)
efLPdib4455 0:f25ac5f8c95e 35 {
efLPdib4455 1:f06777cfcc8d 36 Hoge hoge;
efLPdib4455 2:d28f9138ca4b 37 pc.printf("+(th2mot).. \n\r");
efLPdib4455 1:f06777cfcc8d 38 // truncates th*100. to uint16_t, which is then packed to dseg[6][7].
efLPdib4455 1:f06777cfcc8d 39 // e.g. th=12.345[deg], then hoge.ii=1234 (=0x04d2) and dseg[6]=0xd2, dseg[7]=0x04
efLPdib4455 2:d28f9138ca4b 40 // follows the little endian
efLPdib4455 2:d28f9138ca4b 41 hoge.ii=(uint16_t)(th*100.); dseg[6]=hoge.c[0]; dseg[7]=hoge.c[1];
efLPdib4455 1:f06777cfcc8d 42
efLPdib4455 0:f25ac5f8c95e 43 if( can1.write(CANMessage(1, dseg, 8, CANData, CANStandard)) ) {
efLPdib4455 2:d28f9138ca4b 44 c_sent++; led1 = !led1;
efLPdib4455 2:d28f9138ca4b 45 pc.printf(" ++sent thru can1. c_sent=%d dseg[6][7]=0x%02X%02X \n\r", c_sent, dseg[6],dseg[7]); }
efLPdib4455 0:f25ac5f8c95e 46 else
efLPdib4455 2:d28f9138ca4b 47 pc.printf(" ++failed can1.write sent=%d dseg[6][7]=0x%02X%02X \n\r", c_sent, dseg[6],dseg[7]);
efLPdib4455 1:f06777cfcc8d 48 } // endof th2mot
efLPdib4455 0:f25ac5f8c95e 49
efLPdib4455 2:d28f9138ca4b 50
efLPdib4455 0:f25ac5f8c95e 51 int main()
efLPdib4455 0:f25ac5f8c95e 52 {
efLPdib4455 2:d28f9138ca4b 53 double th; unsigned int id;
efLPdib4455 2:d28f9138ca4b 54 CANMessage msg; // remote msg? what determines the size(msg)?
efLPdib4455 0:f25ac5f8c95e 55 can1.frequency(1000000); can2.frequency(1000000); // CANbus=1Mbps
efLPdib4455 1:f06777cfcc8d 56 th=12.3456; id=1;
efLPdib4455 3:43a6f6157fcf 57 // 9600(default) 115200(not max?) 921600(on STM32F446, benkatz)
efLPdib4455 3:43a6f6157fcf 58 pc.baud(921600);
efLPdib4455 3:43a6f6157fcf 59
efLPdib4455 3:43a6f6157fcf 60 while(1){
efLPdib4455 3:43a6f6157fcf 61 pc.printf("+++(main of th2mot)... +feed theta(double)[deg][0.-360.]");
efLPdib4455 3:43a6f6157fcf 62 pc.scanf("%lf", &th);
efLPdib4455 1:f06777cfcc8d 63
efLPdib4455 3:43a6f6157fcf 64 th2mot(th, id);
efLPdib4455 3:43a6f6157fcf 65 wait(0.2); // dummy wait 0.2[s]
efLPdib4455 3:43a6f6157fcf 66 if(can1.read(msg)) { // org=can2
efLPdib4455 3:43a6f6157fcf 67 c_read++; led2 = !led2;
efLPdib4455 3:43a6f6157fcf 68 pc.printf(" ++can2 c_read=%d, d[0..7]_read=(0x", c_read);
efLPdib4455 3:43a6f6157fcf 69 for(int i=0; i<8; i++) pc.printf("%02X ", msg.data[i]);
efLPdib4455 3:43a6f6157fcf 70 pc.printf(") \n\r");
efLPdib4455 3:43a6f6157fcf 71 } // endof can1.read
efLPdib4455 3:43a6f6157fcf 72 else
efLPdib4455 3:43a6f6157fcf 73 pc.printf(" ++failed can1.read... c_sent=%d\n\r", c_sent);
efLPdib4455 3:43a6f6157fcf 74 } // endof while(1)-loop
efLPdib4455 2:d28f9138ca4b 75
efLPdib4455 2:d28f9138ca4b 76 } // endof main
efLPdib4455 1:f06777cfcc8d 77