Takahashi Shu / Mbed 2 deprecated 1-CAN-th2mot-mbed2-01_02

Dependencies:   mbed

Committer:
efLPdib4455
Date:
Mon Sep 07 04:38:29 2020 +0000
Revision:
1:f06777cfcc8d
Parent:
0:f25ac5f8c95e
Child:
2:d28f9138ca4b
incomplete BUT publish anyway to make the src accessible (r020907).

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 1:f06777cfcc8d 2 // 1--CAN-th2mot-mbed2-01_01
efLPdib4455 1:f06777cfcc8d 3 // packs the position(0-36000) command to the data_segment and
efLPdib4455 1:f06777cfcc8d 4 // dispatches the std_data_frame to the HT02(=GI8008) motor thru CAN1.
efLPdib4455 1:f06777cfcc8d 5 // returns the received data_seg from the motor.
efLPdib4455 1:f06777cfcc8d 6 // (the actual HT02 motor would send back the response data frame?)
efLPdib4455 0:f25ac5f8c95e 7 //
efLPdib4455 1:f06777cfcc8d 8 // th2mot(double th, unsigned int id) (th=theta_in, id=CANid_in)
efLPdib4455 1:f06777cfcc8d 9 //
efLPdib4455 0:f25ac5f8c95e 10
efLPdib4455 0:f25ac5f8c95e 11
efLPdib4455 1:f06777cfcc8d 12 // Ticker ticker;
efLPdib4455 0:f25ac5f8c95e 13 DigitalOut led1(LED1);
efLPdib4455 0:f25ac5f8c95e 14 DigitalOut led2(LED2);
efLPdib4455 0:f25ac5f8c95e 15
efLPdib4455 0:f25ac5f8c95e 16 CAN can1(p30, p29); // (RX,TX)pins in this order
efLPdib4455 0:f25ac5f8c95e 17 CAN can2( p9, p10);
efLPdib4455 0:f25ac5f8c95e 18
efLPdib4455 1:f06777cfcc8d 19 char c_sent=0, c_read=0; // counters of sent and read
efLPdib4455 1:f06777cfcc8d 20 char dseg[8]={0xFF, 0x01, 0x00, 0x64, 0x00, 0x80, 0x00, 0x00}; //data_segment
efLPdib4455 1:f06777cfcc8d 21
efLPdib4455 1:f06777cfcc8d 22 // bytewise RW by the union Hoge, where char MUST be unsigned.
efLPdib4455 1:f06777cfcc8d 23 // short=(signed 2 bytes integer).
efLPdib4455 1:f06777cfcc8d 24 union Hoge{ unsigned char c[2]; uint16_t ii; };
efLPdib4455 1:f06777cfcc8d 25
efLPdib4455 1:f06777cfcc8d 26
efLPdib4455 1:f06777cfcc8d 27
efLPdib4455 0:f25ac5f8c95e 28 // data segment specific to the HT02 motor
efLPdib4455 1:f06777cfcc8d 29 // [0]=host_addr(0xff=this_MCU, 0x01=motor_#01),
efLPdib4455 1:f06777cfcc8d 30 // [1]=func_code(01=pos.cmd), [2]=[4]=reserved(?)
efLPdib4455 1:f06777cfcc8d 31 // [3]=acce=*256[rpm/s], [5]=target velo.[rpm] (of motor axis wo the gear, maybe)
efLPdib4455 1:f06777cfcc8d 32 // [6]=pos.lower byte, [7]=pos.higher byte (of output axis, maybe)[0-36000]
efLPdib4455 1:f06777cfcc8d 33
efLPdib4455 1:f06777cfcc8d 34
efLPdib4455 1:f06777cfcc8d 35 void th2mot(double th, unsigned int id)
efLPdib4455 0:f25ac5f8c95e 36 {
efLPdib4455 1:f06777cfcc8d 37 Hoge hoge;
efLPdib4455 1:f06777cfcc8d 38 printf("+(th2mot).. \n\r");
efLPdib4455 1:f06777cfcc8d 39 // truncates th*100. to uint16_t, which is then packed to dseg[6][7].
efLPdib4455 1:f06777cfcc8d 40 // e.g. th=12.345[deg], then hoge.ii=1234 (=0x04d2) and dseg[6]=0xd2, dseg[7]=0x04
efLPdib4455 1:f06777cfcc8d 41 hoge.ii=(uint16_t)(th*100.); dseg[6]=hoge.c[1]; dseg[7]=hoge.c[0];
efLPdib4455 1:f06777cfcc8d 42
efLPdib4455 0:f25ac5f8c95e 43 if( can1.write(CANMessage(1, dseg, 8, CANData, CANStandard)) ) {
efLPdib4455 0:f25ac5f8c95e 44 c_sent++;
efLPdib4455 1:f06777cfcc8d 45 printf(" ++sent thru can1. c_sent=%d dseg[6][7]=%02x%02X \n\r", c_sent, dseg[6],dseg[7]); }
efLPdib4455 0:f25ac5f8c95e 46 else
efLPdib4455 1:f06777cfcc8d 47 printf(" ++failed can1.write sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
efLPdib4455 0:f25ac5f8c95e 48
efLPdib4455 0:f25ac5f8c95e 49 led1 = !led1;
efLPdib4455 1:f06777cfcc8d 50 } // endof th2mot
efLPdib4455 0:f25ac5f8c95e 51
efLPdib4455 0:f25ac5f8c95e 52 int main()
efLPdib4455 0:f25ac5f8c95e 53 {
efLPdib4455 1:f06777cfcc8d 54 printf("+entered (testmain of th2mot)... \n\n\n\r");
efLPdib4455 0:f25ac5f8c95e 55 can1.frequency(1000000); can2.frequency(1000000); // CANbus=1Mbps
efLPdib4455 1:f06777cfcc8d 56 double th; unsigned int id;
efLPdib4455 1:f06777cfcc8d 57 th=12.3456; id=1;
efLPdib4455 1:f06777cfcc8d 58 th2mot(th, id);
efLPdib4455 0:f25ac5f8c95e 59 CANMessage msg; // remote msg? what determines the size(msg)?
efLPdib4455 0:f25ac5f8c95e 60 while(1){
efLPdib4455 1:f06777cfcc8d 61 printf("+(main) in while loop... \n\r");
efLPdib4455 1:f06777cfcc8d 62 if (can1.read(msg)) { // org=can2
efLPdib4455 0:f25ac5f8c95e 63 c_read++;
efLPdib4455 1:f06777cfcc8d 64 printf(" ++can2 c_read=%d, d[0]_read=%d\n", c_read, msg.data[0]);
efLPdib4455 0:f25ac5f8c95e 65 led2 = !led2;
efLPdib4455 0:f25ac5f8c95e 66 }
efLPdib4455 0:f25ac5f8c95e 67 else
efLPdib4455 1:f06777cfcc8d 68 printf(" ++failed can1.read... sent=%d\n", c_sent);
efLPdib4455 1:f06777cfcc8d 69
efLPdib4455 0:f25ac5f8c95e 70 wait(0.2);
efLPdib4455 1:f06777cfcc8d 71 } // endof infinite_while_loop
efLPdib4455 0:f25ac5f8c95e 72 } // endof main
efLPdib4455 1:f06777cfcc8d 73