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

Dependencies:   mbed

Revision:
1:f06777cfcc8d
Parent:
0:f25ac5f8c95e
Child:
2:d28f9138ca4b
--- a/main.cpp	Sun Sep 06 05:53:03 2020 +0000
+++ b/main.cpp	Mon Sep 07 04:38:29 2020 +0000
@@ -1,80 +1,73 @@
 // 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.  
+// 1--CAN-th2mot-mbed2-01_01  
+//    packs the position(0-36000) command to the data_segment and 
+//    dispatches the std_data_frame to the HT02(=GI8008) motor thru CAN1.
+//    returns the received data_seg from the motor.
+//    (the actual HT02 motor would send back the response data frame?)
 //
-//   (the actual HT02 motor would send back the response data frame?)
+//  th2mot(double th, unsigned int id)    (th=theta_in, id=CANid_in) 
+//
 
-//#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;
+// 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};
-//
+char c_sent=0, c_read=0;   // counters of sent and read
+char dseg[8]={0xFF, 0x01, 0x00, 0x64, 0x00, 0x80, 0x00, 0x00};  //data_segment
+
+//  bytewise RW by the union Hoge, where char MUST be unsigned.
+//  short=(signed 2 bytes integer). 
+union Hoge{ unsigned char c[2]; uint16_t ii; };
+
+
+
 // 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()
+// [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 motor axis wo the gear, maybe)
+// [6]=pos.lower byte,      [7]=pos.higher byte (of output axis, maybe)[0-36000]
+
+
+void th2mot(double th, unsigned int id)
 {
-    printf("+sending can1... \n\r");
+    Hoge hoge;
+    printf("+(th2mot).. \n\r");
+    //  truncates th*100. to uint16_t, which is then packed to dseg[6][7].
+    //  e.g. th=12.345[deg], then hoge.ii=1234 (=0x04d2) and dseg[6]=0xd2, dseg[7]=0x04
+    hoge.ii=(uint16_t)(th*100.);         dseg[6]=hoge.c[1]; dseg[7]=hoge.c[0];
+
     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]);  }
+        printf(" ++sent thru can1. c_sent=%d dseg[6][7]=%02x%02X \n\r", c_sent, dseg[6],dseg[7]);  }
     else
-        printf(" ++failed can1.write c_sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
+        printf(" ++failed can1.write sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
  
     led1 = !led1;
-}   //  endof send       
+}   //  endof th2mot
 
 int main()
 {
-    printf("+entered main. attached send to the ticker (of mbed_os_2) every 1s. \n\r");
+    printf("+entered (testmain of th2mot)... \n\n\n\r");
     can1.frequency(1000000);    can2.frequency(1000000);  // CANbus=1Mbps
-    ticker.attach(&send, 1);
+    double th; unsigned int id;
+    th=12.3456;  id=1;
+    th2mot(th, id);
     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
+        printf("+(main) in while loop...  \n\r");
+        if (can1.read(msg)) {   // org=can2
             c_read++;
-            printf(" ++fetched can2. c_read=%d, d[7]_read=%d\n\r", c_read, msg.data[7]);
+            printf(" ++can2 c_read=%d, d[0]_read=%d\n", c_read, msg.data[0]);
             led2 = !led2;
         }
         else
-            printf(" ++failed can2.read...  sent=%d\n\r", c_sent);
+            printf(" ++failed can1.read...  sent=%d\n", c_sent);
+
         wait(0.2);
-//      ThisThread::sleep_for(200);   // in [ms], maybe.. ThisThread in rtos?
-    }   //  endof inf_while
+    }   //  endof infinite_while_loop
 }       //  endof main
+