Motor Controller 200320A API

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Picmon
Date:
Fri Mar 20 10:54:58 2020 +0000
Commit message:
motor controller 200320A

Changed in this revision

MotorControl.cpp Show annotated file Show diff for this revision Revisions of this file
MotorControl.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 333434a8611b MotorControl.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorControl.cpp	Fri Mar 20 10:54:58 2020 +0000
@@ -0,0 +1,149 @@
+#include "mbed.h"
+#include "MotorControl.h"
+
+RawSerial mc_usart(PA_2,PA_15,115200);// this will be USART6 for motor control 
+
+const string rxMsgTable[MESSAGE] = {
+
+    "SET DRIVE WORKING SETINGS",
+    "SET FEEDBACK BOOST CURRENT",    
+    
+};
+
+struct drvWorkingSettings{
+ 
+ bool par0;
+ bool par1;
+ bool par2;
+ bool par3;
+ bool feedBackMotChk;
+ bool par5;
+ bool auomaticMotCurRed;
+ bool disableDigOutputsFWH;
+ bool par8; 
+ bool par9;
+ bool motRotDir;
+ bool par11;
+ bool par12; 
+ bool fbErrMotAct;
+ bool par14;
+ bool par15;  
+  
+};
+
+char rxMsgStore[BUFFER_SIZE];//received local message store
+char mc_Tx_Buffer[BUFFER_SIZE];//transmitted message buffer 
+volatile char mc_Rx_Buffer[BUFFER_SIZE];//received message buffer
+volatile unsigned char mc_Rx_Rd_Ptr = 0;//circulare buffer read pointer
+volatile unsigned char mc_Rx_Wr_Ptr = 0;//circulare buffer write pointer
+unsigned int rx_int = 0;//received integer / character
+
+//unsigned char i; 
+bool msgRecFlag = false;//message received flag 
+
+void motMsg_RX_ISR(void){
+    
+    //led1=1;
+    //save character in buffer
+    mc_Rx_Buffer[mc_Rx_Wr_Ptr] = mc_usart.getc();
+    //increment pointer
+    mc_Rx_Wr_Ptr++;
+    //test for wrap around
+    if(mc_Rx_Wr_Ptr == (BUFFER_SIZE - 1))
+    {
+        mc_Rx_Wr_Ptr = 0;
+    }
+    //wait(0.0001);
+    //led1=0; 
+}
+  
+bool getMotMsg(char * msgArray){
+    
+    //declare variables
+    static unsigned char i;
+    unsigned int uiMsg= 0xFFFF;
+
+    //test for any characters in the receiver buffer
+    if (mc_Rx_Wr_Ptr != mc_Rx_Rd_Ptr)
+    {
+        //character in the buffer, return the value
+        uiMsg = mc_Rx_Buffer[mc_Rx_Rd_Ptr];
+        //increment the Rd pointer
+        mc_Rx_Rd_Ptr++;
+        //test for wrap around
+        if(mc_Rx_Rd_Ptr == (BUFFER_SIZE - 1))
+        {
+            mc_Rx_Rd_Ptr = 0;
+        }
+    }
+                
+    if(uiMsg != 0xFFFF){//check that a character is in the buffer        
+        msgArray[i++] = uiMsg;//store characters in local message store array
+        
+        mc_usart.printf("%c\r\n", uiMsg); 
+               
+        if(uiMsg == '\r'){//look for <CR> then print out the message in the RX buffer
+            
+            msgRecFlag = true;//set the message received flag
+            
+            mc_usart.printf("Stored Message\r\n");
+                            
+            for(i=0 ; i <BUFFER_SIZE ; i++)                    
+                mc_usart.printf("%c\r\n",msgArray[i]);
+                
+        }
+    }
+    else
+        i=0;    
+            
+             
+    return(msgRecFlag);
+} 
+
+void motMsgDecode(unsigned int rxMsgTable[], unsigned int rxMsg[]){
+    
+    if(memcmp(rxMsgTable, rxMsg, 10)){
+        
+    }
+    
+}
+
+void clrRxMsg(char array[]){
+
+    for(unsigned char i=0; i<BUFFER_SIZE; i++)
+        array[i]=0;
+    
+    #ifdef DEBUG    
+        mc_usart.printf("Clear the RX local message store\r\n"); 
+    #endif
+                
+    for(unsigned char i=0; i<BUFFER_SIZE; i++){
+    
+        #ifdef DEBUG
+            mc_usart.printf("%c\r\n",array[i]);  
+        #endif
+    }
+} 
+
+
+bool moveMot(uint8_t par1, int32_t par2){
+    
+    static uint8_t newPar1 = 0;
+    static int32_t newPar2 = 0;    
+    bool fault = false;
+    
+    if((par1 != newPar1) || (par2 != newPar2)){
+        
+        newPar1 = par1;
+        newPar2 = par2;        
+        
+        if(((par1 >=0) && (par1 <=4))&&((par2 >= REV_MAX_STEPS) && (par2 <= FWD_MAX_STEPS))){  
+            sprintf(mc_Tx_Buffer,"MOVEMOT %d %u\r",par1, par2);      
+            mc_usart.printf("%s\r",mc_Tx_Buffer);    
+        }
+        else
+            fault = true;
+                            
+    }
+    return(fault);    
+}
diff -r 000000000000 -r 333434a8611b MotorControl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorControl.h	Fri Mar 20 10:54:58 2020 +0000
@@ -0,0 +1,36 @@
+#ifndef MOTORCONTROL_H
+#define MOTORCONTROL_H
+
+#include "mbed.h"
+#include <string> 
+
+#define DEBUG_MOTOR
+
+#define BUFFER_SIZE  32
+#define MESSAGE 20
+
+#define REV_MAX_STEPS -2147483648
+#define FWD_MAX_STEPS 2147483648
+#define DEBUG
+ 
+
+extern RawSerial mc_usart;// this will be USART6 for motor control 
+extern const string rxMsgTable[];
+extern char rxMsgStore[];//received local message store
+extern char mc_Tx_Buffer[];//transmitted message buffer 
+extern volatile char mc_Rx_Buffer[];//received message buffer
+extern volatile unsigned char mc_Rx_Rd_Ptr;//circulare buffer read pointer
+extern volatile unsigned char mc_Rx_Wr_Ptr;//circulare buffer write pointer
+extern unsigned int rx_int;//received integer / character
+
+//unsigned char i; 
+extern bool msgRecFlag;//message received flag 
+
+void motMsg_RX_ISR(void);
+bool getMotMsg(char * msgArray);
+void motMsgDecode(unsigned int rxMsgTable[], unsigned int rxMsg[]);void clrRxMsg(char array[]);
+bool moveMot(uint8_t par1, int32_t par2);
+
+
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 333434a8611b main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Mar 20 10:54:58 2020 +0000
@@ -0,0 +1,55 @@
+
+/*
+
+    ED Motor Controller API
+    
+    
+    Notes:
+    A robust protocal has been called for, the motor command sent out is to be echoed back with and <ACK> or <NACK>
+       
+    In order to compare what was sent to what is echoed back two buffers are required, a TX buffer and an RX buffer, they can then be compared 
+    using memcmp() function
+    
+    19/03/20 Compare of tx and rx buffers can be done
+    20/03/20 Migrate code to mbed studio
+
+
+
+*/
+
+#include "mbed.h"
+#include <string> 
+#include "MotorControl.h"
+
+DigitalOut led1(LED1);
+
+
+
+int main(){
+    
+    led1=0;
+    
+    mc_usart.attach(&motMsg_RX_ISR);//start the serial receiver interrupt
+    mc_usart.printf("ED Motor Controller Commands\r\n");
+       
+    clrRxMsg(rxMsgStore);
+
+    moveMot(1, 1000);
+    
+    mc_usart.printf("TX Buffer Contents\r\n");
+    
+    for(unsigned char i=0 ; i <BUFFER_SIZE ; i++)                     
+        mc_usart.printf("%c\r\n",mc_Tx_Buffer[i]);
+  
+    while(1){
+        
+        if(getMotMsg(rxMsgStore)){//get the message from the receive buffer and store it in local message store
+            
+            msgRecFlag = false;//clear the message received flag
+            
+            if(memcmp(rxMsgStore,mc_Tx_Buffer,strlen(rxMsgStore)) == 0){
+                led1=1;
+            }            
+        }                      
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 333434a8611b main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Fri Mar 20 10:54:58 2020 +0000
@@ -0,0 +1,7 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+#include "mbed.h"
+
+
+#endif 
\ No newline at end of file
diff -r 000000000000 -r 333434a8611b mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 20 10:54:58 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file