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

Dependencies:   mbed

Revision:
2:d28f9138ca4b
Parent:
1:f06777cfcc8d
Child:
3:43a6f6157fcf
--- a/main.cpp	Mon Sep 07 04:38:29 2020 +0000
+++ b/main.cpp	Wed Sep 09 10:36:36 2020 +0000
@@ -1,4 +1,5 @@
-// mbed-os rev.171(mbed_os=2, very old)  (r020904,sige)
+//
+// mbed-os rev.171(mbed2, very old, deprecated BUT useful)  (r020904,sige)
 // 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.
@@ -7,11 +8,19 @@
 //
 //  th2mot(double th, unsigned int id)    (th=theta_in, id=CANid_in) 
 //
+// 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 motor axis wo the gear, maybe)
+// [6]=pos.lower byte,      [7]=pos.higher byte (of output axis, maybe)[0-36000]
 
+#include "mbed.h"
+#include "CAN.h"
 
 // Ticker ticker;
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
+Serial pc(USBTX, USBRX);
 
 CAN can1(p30, p29);     // (RX,TX)pins in this order
 CAN can2( p9, p10);
@@ -19,55 +28,49 @@
 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). 
+//  byte manipu using the union Hoge, char MUST be unsigned.
 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 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)
 {
     Hoge hoge;
-    printf("+(th2mot).. \n\r");
+    pc.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];
+    //  follows the little endian
+    hoge.ii=(uint16_t)(th*100.);         dseg[6]=hoge.c[0]; dseg[7]=hoge.c[1];
 
     if( can1.write(CANMessage(1, dseg, 8, CANData, CANStandard)) ) {
-        c_sent++;
-        printf(" ++sent thru can1. c_sent=%d dseg[6][7]=%02x%02X \n\r", c_sent, dseg[6],dseg[7]);  }
+        c_sent++;   led1 = !led1;
+        pc.printf(" ++sent thru can1. c_sent=%d dseg[6][7]=0x%02X%02X \n\r", c_sent, dseg[6],dseg[7]);  }
     else
-        printf(" ++failed can1.write sent=%d dseg[7]=%02x\n\r", c_sent, dseg[7]);
- 
-    led1 = !led1;
+        pc.printf(" ++failed can1.write sent=%d dseg[6][7]=0x%02X%02X \n\r", c_sent, dseg[6],dseg[7]);
 }   //  endof th2mot
 
+
 int main()
 {
-    printf("+entered (testmain of th2mot)... \n\n\n\r");
+    double th; unsigned int id;
+    CANMessage msg;      // remote msg? what determines the size(msg)?
     can1.frequency(1000000);    can2.frequency(1000000);  // CANbus=1Mbps
-    double th; unsigned int id;
     th=12.3456;  id=1;
-    th2mot(th, id);
-    CANMessage msg;      // remote msg? what determines the size(msg)?
+//   pc.baud(115200);    // 9600(default) 115200(max, maybe)
+
     while(1){
-        printf("+(main) in while loop...  \n\r");
-        if (can1.read(msg)) {   // org=can2
-            c_read++;
-            printf(" ++can2 c_read=%d, d[0]_read=%d\n", c_read, msg.data[0]);
-            led2 = !led2;
-        }
-        else
-            printf(" ++failed can1.read...  sent=%d\n", c_sent);
+      pc.printf("+++(main of th2mot)...  +feed theta(double)[deg][0.-360.]");
+      pc.scanf("%lf", &th);
 
-        wait(0.2);
-    }   //  endof infinite_while_loop
-}       //  endof main
+      th2mot(th, id);
+      wait(0.2);             // dummy wait 0.2[s]
+      if(can1.read(msg)) {   // org=can2
+         c_read++; led2 = !led2;
+         pc.printf(" ++can2 c_read=%d, d[0..7]_read=(0x", c_read);
+         for(int i=0; i<8; i++) pc.printf("%02X ", msg.data[i]);
+         pc.printf(") \n\r");
+      }    //  endof can1.read
+      else
+         pc.printf(" ++failed can1.read...  c_sent=%d\n\r", c_sent);
+    }      //  endof while(1)-loop
+     
+}          //  endof main