Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:d28f9138ca4b, 2020-09-09 (annotated)
- Committer:
- efLPdib4455
- Date:
- Wed Sep 09 10:36:36 2020 +0000
- Revision:
- 2:d28f9138ca4b
- Parent:
- 1:f06777cfcc8d
- Child:
- 3:43a6f6157fcf
(use mbed2) +theta to MIT mini cheetah motor r020909
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:d28f9138ca4b | 57 | // pc.baud(115200); // 9600(default) 115200(max, maybe) |
efLPdib4455 | 2:d28f9138ca4b | 58 | |
efLPdib4455 | 0:f25ac5f8c95e | 59 | while(1){ |
efLPdib4455 | 2:d28f9138ca4b | 60 | pc.printf("+++(main of th2mot)... +feed theta(double)[deg][0.-360.]"); |
efLPdib4455 | 2:d28f9138ca4b | 61 | pc.scanf("%lf", &th); |
efLPdib4455 | 1:f06777cfcc8d | 62 | |
efLPdib4455 | 2:d28f9138ca4b | 63 | th2mot(th, id); |
efLPdib4455 | 2:d28f9138ca4b | 64 | wait(0.2); // dummy wait 0.2[s] |
efLPdib4455 | 2:d28f9138ca4b | 65 | if(can1.read(msg)) { // org=can2 |
efLPdib4455 | 2:d28f9138ca4b | 66 | c_read++; led2 = !led2; |
efLPdib4455 | 2:d28f9138ca4b | 67 | pc.printf(" ++can2 c_read=%d, d[0..7]_read=(0x", c_read); |
efLPdib4455 | 2:d28f9138ca4b | 68 | for(int i=0; i<8; i++) pc.printf("%02X ", msg.data[i]); |
efLPdib4455 | 2:d28f9138ca4b | 69 | pc.printf(") \n\r"); |
efLPdib4455 | 2:d28f9138ca4b | 70 | } // endof can1.read |
efLPdib4455 | 2:d28f9138ca4b | 71 | else |
efLPdib4455 | 2:d28f9138ca4b | 72 | pc.printf(" ++failed can1.read... c_sent=%d\n\r", c_sent); |
efLPdib4455 | 2:d28f9138ca4b | 73 | } // endof while(1)-loop |
efLPdib4455 | 2:d28f9138ca4b | 74 | |
efLPdib4455 | 2:d28f9138ca4b | 75 | } // endof main |
efLPdib4455 | 1:f06777cfcc8d | 76 |