J L / Mbed 2 deprecated ringBuffer26a

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 EOL SEQUENCE = NONE ON YAT, PROTOCOL WILL FAIL IF THIS IS NOT DONE
00004 
00005 */
00006 
00007 
00008 #include "mbed.h"
00009 #include <string> 
00010 #include "MotorControl.h"
00011 #include "readCommands.h"
00012 #include "writeCommands.h"
00013 #include "main.h"
00014 
00015 //================================================================================================================================
00016 // COMPANY                :  Verderflex
00017 //
00018 // PROJECT                :  ED Motor Controler API
00019 // DATE                   :  11th May 2020   
00020 
00021 string HARDWARE =           "Hardware Version : X1.00";                                                
00022 string SOFTWARE =           "Software Version : X1.00";
00023 string AUTHOR   =           "Author : Jim Lowe";
00024 
00025 /*
00026  *   Firmware Numbering Scheme, V Major.Minor.Bugfix
00027  *
00028  *   Major  : Major Changes to function
00029  *   Minor  : Minor Changes to function
00030  *   Bugfix : Firmware fixes to function
00031  *
00032  */
00033 
00034 // HARDWARE              :   NUCLEO-F746ZG + Nextion 4.3" Display
00035 //
00036 // COMPILER              :   Mbed Studio
00037 
00038 //================================================================================================================================
00039 //************************************************  Code Status / Changes ********************************************************
00040 //================================================================================================================================
00041 
00042 
00043 /*
00044 
00045     ED Motor Controller API
00046     
00047     
00048     Notes:
00049     A robust protocal has been called for, the motor command sent out is to be echoed back with and <ACK> or <NACK>
00050        
00051     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 
00052     using memcmp() function
00053     
00054     19/03/20    Compare of tx and rx buffers can be done
00055     20/03/20    Migrate code to mbed studio
00056     20/03/20    Code cannot be migrated to mbed studio as the target does not use mbed OS 5
00057     23/03/20    memcmp() and memcpy() implemented and working, comparision of tx and rx buffers can be done
00058     26/03/20    setSpeed(),setTorque(),setRotMotor(),set_mL() write commands tested and working with echo compare
00059     26/03/20_D  ACK and NAK added, rx message checked for <CR><ACK> or <NAK>
00060     27/03/20_D  Sorting of received massages into register type, echo type and NAK seems to be working
00061     30/03/20_B  Process Motor Message function created that combines all message types that can be transmitted and received
00062     31/03/20_D  LED1=1 indication validates data automatically, if data is invalid LED1=0 
00063     03/04/20_A  Setup another serial port required for testing API, one for Debug and the other to communicate with Sagittarius drive
00064     07/04/20_A  Simplified so that RX ISR collects the data and stores in the required memory location
00065     08/04/20_B  Software manual IMD11 V00R02 changes need to be added to software, this is a lot of work as we now have to collect a lot of bytes returnd from the motor controller
00066     11/05/20_A  Back to work,
00067 */
00068 //What is sent is received exactly, no problems
00069 
00070 RawSerial mc_debug(USBTX, USBRX, MED_BAUD);//debug port PA2_TX, PA15_RX
00071 RawSerial mc_usart(PA_9,PA_10,MED_BAUD);//motor controller communication port
00072 //RawSerial mc_debug(PA_2,PB_4,MED_BAUD);//motor controller communication port
00073 
00074 //RawSerial mc_debug(PA_9,PA_10,MED_BAUD);//motor controller communication port
00075 //RawSerial mc_usart(USBTX, USBRX, MED_BAUD);//debug port PA2_TX, PA15_RX
00076 
00077 Timer tBtn;
00078 
00079 DigitalOut led1(LED1);
00080 
00081 Timer t1;
00082 
00083 uint8_t msgResend = false;
00084 
00085 float n_speed = 0.001;
00086 
00087 uint8_t cmdCnt=0;
00088 bool rxMsgPending = false;
00089 
00090 uint32_t speed=0;
00091 
00092 int main(){//1
00093 
00094     Init();
00095        
00096     //setup motor control
00097     a7_out1 = 1;//motor hardware enable
00098     setTorque(3456, mc_Tx_Buffer);//set torque 3.456N.m  
00099     setRotMotor(CCW, mc_Tx_Buffer);//set motor CCW rotation
00100     n_speed = 123.456;
00101     setSpeed(&n_speed, mc_Tx_Buffer);
00102     
00103     tBtn.start();
00104     
00105     t1.start();
00106     
00107     while(1){//2
00108     
00109         /*identify the last message sent    
00110         
00111           the mc_Tx_Buffer contain the last message sent out which has failed
00112           this message need to be resent a number of times (3 tries)
00113         
00114         */
00115 
00116         if(tBtn.read() > 0.100){//every x read the buttons
00117             
00118             tBtn.reset();
00119             
00120             if(d3_button){//speed inc
00121                 if(t1.read() > 1.0){
00122                     t1.reset();
00123                     n_speed+=10;
00124 
00125                 }
00126                 n_speed+=0.001;
00127                 setSpeed(&n_speed, mc_Tx_Buffer);
00128             }
00129             else
00130             if(d4_button){//speed dec
00131                 if(t1.read() > 1.0){
00132                     t1.reset();
00133                     n_speed-=10;
00134                 }
00135                 n_speed-=0.001;
00136                 setSpeed(&n_speed, mc_Tx_Buffer);
00137             }
00138             else
00139             if(d5_button){//stop
00140                stopMot(STOP_NO_RAMP, NOT_USED,  mc_Tx_Buffer); 
00141             }
00142             else
00143             if(d6_button){
00144                 
00145             
00146             }
00147             else
00148             if(d7_button){
00149                 
00150             
00151             }
00152             else
00153             if(d3_button){
00154                 
00155             
00156             }
00157             else
00158             if(d8_button){
00159                 
00160             
00161             }
00162             else
00163             if(d9_button){
00164                 
00165             
00166             }
00167             else
00168             if(d10_button){
00169                 
00170             
00171             }
00172             else
00173             if(d11_button){
00174                 
00175             
00176             }
00177             else   
00178             if(d12_button){
00179                 
00180             
00181             }
00182             else
00183                 t1.reset();
00184         }
00185 
00186         //mc_debug.printf("cmdCnt = %d", cmdCnt);
00187 /*           
00188         switch(cmdCnt){    
00189             case 0: setRotMotor(CCW, mc_Tx_Buffer);break;//free run the motor CCW 
00190             case 1: setSpeed(123456, mc_Tx_Buffer);break;//123.456 RPM  
00191             case 2: setRotMotor(CW, mc_Tx_Buffer);break;//free run the motor CW
00192             case 3: setTorque(3456, mc_Tx_Buffer);break;//3.456N.m 
00193             case 4: setTorque(7000, mc_Tx_Buffer);break;//7N.m    
00194             case 5: set_mL(CW, 1000, mc_Tx_Buffer);break;//dispence 1000ml running CW
00195             case 6: set_mL(CCW, 1000, mc_Tx_Buffer);break;//dispence 1000ml running CW
00196         }
00197 */        
00198 /*           
00199         switch(cmdCnt){
00200             case READ_DIG_IN:               readDigInput(mc_Tx_Buffer);break;
00201             case READ_DIG_OUT:              readDigOutput(mc_Tx_Buffer);break;
00202             case READ_CURRENT:              readCurrent(mc_Tx_Buffer);break;
00203             case READ_DRV_REG:              readDrvReg(mc_Tx_Buffer);break;
00204             case READ_DRV_REG_EXT:          readDrvRegExt(mc_Tx_Buffer);break;
00205             case READ_DRV_TEMP:             readTemp(mc_Tx_Buffer);break;
00206             case READ_DRV_VOLTAGE:          readVoltage(mc_Tx_Buffer);break;
00207             case READ_DRV_WORKING_SET:      readDrvWorkSet(mc_Tx_Buffer);break;
00208             case READ_DRV_WORKING_SET_EXT:  readDrvWorkSetExt(mc_Tx_Buffer);break; 
00209             case READ_ERROR_REG:            readErrReg(mc_Tx_Buffer);break;
00210             case READ_FB_BOOST_CUR:         readFBboostCur(mc_Tx_Buffer);break;
00211             case READ_FB_STATUS:            readFBstat(mc_Tx_Buffer);break;
00212             case READ_FW_VERSION:           readFWVwersion(mc_Tx_Buffer);break;
00213             case READ_FW_CHECKSUM:          readFWChecksum(mc_Tx_Buffer);break;
00214             case READ_MASTER_REG:           readMastReg(mc_Tx_Buffer);break;   
00215             case READ_MIN_CURRENT:          readMinCur(mc_Tx_Buffer);break;
00216             case READ_MAX_CURRENT:          readMaxCur(mc_Tx_Buffer);break;
00217             case READ_BOOST_CURRENT:        readBoostCur(mc_Tx_Buffer);break;  
00218             case READ_NOM_CURRENT:          readNomCur(mc_Tx_Buffer);break;                
00219         }
00220 */        
00221  /*
00222          if(rxMsgPending == false){   
00223             rxMsgPending = true;
00224             
00225              if(cmdCnt < COMMANDS_TO_TEST) 
00226                 cmdCnt++;
00227             else
00228                 cmdCnt=0;     
00229         }
00230 */        
00231 
00232         //while(getMotMsg(mc_Tx_Buffer, rxMsgStore) == RX_ECHO_FAIL);//message has failed so resent the last message again and loop a nuber of re-tries if required.
00233     
00234         
00235         if(getMotMsg(mc_Tx_Buffer, rxMsgStore) == REBOOT){
00236             mc_usart.printf("\r\n\r\nRebooting the system\r\n\r\n");  
00237             NVIC_SystemReset(); 
00238         }
00239         
00240         
00241     }//2     
00242 }//1