ringBuffer26

Dependencies:   mbed

Committer:
Picmon
Date:
Mon May 18 19:04:41 2020 +0000
Revision:
1:0cb065f9d55a
Parent:
0:333434a8611b
ring buffer26;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Picmon 1:0cb065f9d55a 1 /*
Picmon 1:0cb065f9d55a 2
Picmon 1:0cb065f9d55a 3 EOL SEQUENCE = NONE ON YAT, PROTOCOL WILL FAIL IF THIS IS NOT DONE
Picmon 1:0cb065f9d55a 4
Picmon 1:0cb065f9d55a 5 */
Picmon 1:0cb065f9d55a 6
Picmon 1:0cb065f9d55a 7
Picmon 1:0cb065f9d55a 8 #include "mbed.h"
Picmon 1:0cb065f9d55a 9 #include <string>
Picmon 1:0cb065f9d55a 10 #include "MotorControl.h"
Picmon 1:0cb065f9d55a 11 #include "readCommands.h"
Picmon 1:0cb065f9d55a 12 #include "writeCommands.h"
Picmon 1:0cb065f9d55a 13 #include "main.h"
Picmon 1:0cb065f9d55a 14
Picmon 1:0cb065f9d55a 15 //================================================================================================================================
Picmon 1:0cb065f9d55a 16 // COMPANY : Verderflex
Picmon 1:0cb065f9d55a 17 //
Picmon 1:0cb065f9d55a 18 // PROJECT : ED Motor Controler API
Picmon 1:0cb065f9d55a 19 // DATE : 11th May 2020
Picmon 1:0cb065f9d55a 20
Picmon 1:0cb065f9d55a 21 string HARDWARE = "Hardware Version : X1.00";
Picmon 1:0cb065f9d55a 22 string SOFTWARE = "Software Version : X1.00";
Picmon 1:0cb065f9d55a 23 string AUTHOR = "Author : Jim Lowe";
Picmon 1:0cb065f9d55a 24
Picmon 1:0cb065f9d55a 25 /*
Picmon 1:0cb065f9d55a 26 * Firmware Numbering Scheme, V Major.Minor.Bugfix
Picmon 1:0cb065f9d55a 27 *
Picmon 1:0cb065f9d55a 28 * Major : Major Changes to function
Picmon 1:0cb065f9d55a 29 * Minor : Minor Changes to function
Picmon 1:0cb065f9d55a 30 * Bugfix : Firmware fixes to function
Picmon 1:0cb065f9d55a 31 *
Picmon 1:0cb065f9d55a 32 */
Picmon 1:0cb065f9d55a 33
Picmon 1:0cb065f9d55a 34 // HARDWARE : NUCLEO-F746ZG + Nextion 4.3" Display
Picmon 1:0cb065f9d55a 35 //
Picmon 1:0cb065f9d55a 36 // COMPILER : Mbed Studio
Picmon 1:0cb065f9d55a 37
Picmon 1:0cb065f9d55a 38 //================================================================================================================================
Picmon 1:0cb065f9d55a 39 //************************************************ Code Status / Changes ********************************************************
Picmon 1:0cb065f9d55a 40 //================================================================================================================================
Picmon 1:0cb065f9d55a 41
Picmon 0:333434a8611b 42
Picmon 0:333434a8611b 43 /*
Picmon 0:333434a8611b 44
Picmon 0:333434a8611b 45 ED Motor Controller API
Picmon 0:333434a8611b 46
Picmon 0:333434a8611b 47
Picmon 0:333434a8611b 48 Notes:
Picmon 0:333434a8611b 49 A robust protocal has been called for, the motor command sent out is to be echoed back with and <ACK> or <NACK>
Picmon 0:333434a8611b 50
Picmon 0:333434a8611b 51 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
Picmon 0:333434a8611b 52 using memcmp() function
Picmon 0:333434a8611b 53
Picmon 1:0cb065f9d55a 54 19/03/20 Compare of tx and rx buffers can be done
Picmon 1:0cb065f9d55a 55 20/03/20 Migrate code to mbed studio
Picmon 1:0cb065f9d55a 56 20/03/20 Code cannot be migrated to mbed studio as the target does not use mbed OS 5
Picmon 1:0cb065f9d55a 57 23/03/20 memcmp() and memcpy() implemented and working, comparision of tx and rx buffers can be done
Picmon 1:0cb065f9d55a 58 26/03/20 setSpeed(),setTorque(),setRotMotor(),set_mL() write commands tested and working with echo compare
Picmon 1:0cb065f9d55a 59 26/03/20_D ACK and NAK added, rx message checked for <CR><ACK> or <NAK>
Picmon 1:0cb065f9d55a 60 27/03/20_D Sorting of received massages into register type, echo type and NAK seems to be working
Picmon 1:0cb065f9d55a 61 30/03/20_B Process Motor Message function created that combines all message types that can be transmitted and received
Picmon 1:0cb065f9d55a 62 31/03/20_D LED1=1 indication validates data automatically, if data is invalid LED1=0
Picmon 1:0cb065f9d55a 63 03/04/20_A Setup another serial port required for testing API, one for Debug and the other to communicate with Sagittarius drive
Picmon 1:0cb065f9d55a 64 07/04/20_A Simplified so that RX ISR collects the data and stores in the required memory location
Picmon 1:0cb065f9d55a 65 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
Picmon 1:0cb065f9d55a 66 11/05/20_A Back to work,
Picmon 1:0cb065f9d55a 67 */
Picmon 1:0cb065f9d55a 68 //What is sent is received exactly, no problems
Picmon 0:333434a8611b 69
Picmon 1:0cb065f9d55a 70 RawSerial mc_debug(USBTX, USBRX, MED_BAUD);//debug port PA2_TX, PA15_RX
Picmon 1:0cb065f9d55a 71 RawSerial mc_usart(PA_9,PA_10,MED_BAUD);//motor controller communication port
Picmon 1:0cb065f9d55a 72 //RawSerial mc_debug(PA_2,PB_4,MED_BAUD);//motor controller communication port
Picmon 0:333434a8611b 73
Picmon 1:0cb065f9d55a 74 //RawSerial mc_debug(PA_9,PA_10,MED_BAUD);//motor controller communication port
Picmon 1:0cb065f9d55a 75 //RawSerial mc_usart(USBTX, USBRX, MED_BAUD);//debug port PA2_TX, PA15_RX
Picmon 1:0cb065f9d55a 76
Picmon 1:0cb065f9d55a 77 Timer tBtn;
Picmon 0:333434a8611b 78
Picmon 0:333434a8611b 79 DigitalOut led1(LED1);
Picmon 0:333434a8611b 80
Picmon 1:0cb065f9d55a 81 Timer t1;
Picmon 0:333434a8611b 82
Picmon 1:0cb065f9d55a 83 uint8_t msgResend = false;
Picmon 1:0cb065f9d55a 84
Picmon 1:0cb065f9d55a 85 float n_speed = 0.001;
Picmon 1:0cb065f9d55a 86
Picmon 1:0cb065f9d55a 87 uint8_t cmdCnt=0;
Picmon 1:0cb065f9d55a 88 bool rxMsgPending = false;
Picmon 1:0cb065f9d55a 89
Picmon 1:0cb065f9d55a 90 uint32_t speed=0;
Picmon 0:333434a8611b 91
Picmon 1:0cb065f9d55a 92 int main(){//1
Picmon 1:0cb065f9d55a 93
Picmon 1:0cb065f9d55a 94 Init();
Picmon 1:0cb065f9d55a 95
Picmon 1:0cb065f9d55a 96 //setup motor control
Picmon 1:0cb065f9d55a 97 a7_out1 = 1;//motor hardware enable
Picmon 1:0cb065f9d55a 98 setTorque(3456, mc_Tx_Buffer);//set torque 3.456N.m
Picmon 1:0cb065f9d55a 99 setRotMotor(CCW, mc_Tx_Buffer);//set motor CCW rotation
Picmon 1:0cb065f9d55a 100 n_speed = 123.456;
Picmon 1:0cb065f9d55a 101 setSpeed(&n_speed, mc_Tx_Buffer);
Picmon 0:333434a8611b 102
Picmon 1:0cb065f9d55a 103 tBtn.start();
Picmon 0:333434a8611b 104
Picmon 1:0cb065f9d55a 105 t1.start();
Picmon 1:0cb065f9d55a 106
Picmon 1:0cb065f9d55a 107 while(1){//2
Picmon 0:333434a8611b 108
Picmon 1:0cb065f9d55a 109 /*identify the last message sent
Picmon 1:0cb065f9d55a 110
Picmon 1:0cb065f9d55a 111 the mc_Tx_Buffer contain the last message sent out which has failed
Picmon 1:0cb065f9d55a 112 this message need to be resent a number of times (3 tries)
Picmon 0:333434a8611b 113
Picmon 1:0cb065f9d55a 114 */
Picmon 1:0cb065f9d55a 115
Picmon 1:0cb065f9d55a 116 if(tBtn.read() > 0.100){//every x read the buttons
Picmon 1:0cb065f9d55a 117
Picmon 1:0cb065f9d55a 118 tBtn.reset();
Picmon 1:0cb065f9d55a 119
Picmon 1:0cb065f9d55a 120 if(d3_button){//speed inc
Picmon 1:0cb065f9d55a 121 if(t1.read() > 1.0){
Picmon 1:0cb065f9d55a 122 t1.reset();
Picmon 1:0cb065f9d55a 123 n_speed+=10;
Picmon 1:0cb065f9d55a 124
Picmon 1:0cb065f9d55a 125 }
Picmon 1:0cb065f9d55a 126 n_speed+=0.001;
Picmon 1:0cb065f9d55a 127 setSpeed(&n_speed, mc_Tx_Buffer);
Picmon 1:0cb065f9d55a 128 }
Picmon 1:0cb065f9d55a 129 else
Picmon 1:0cb065f9d55a 130 if(d4_button){//speed dec
Picmon 1:0cb065f9d55a 131 if(t1.read() > 1.0){
Picmon 1:0cb065f9d55a 132 t1.reset();
Picmon 1:0cb065f9d55a 133 n_speed-=10;
Picmon 1:0cb065f9d55a 134 }
Picmon 1:0cb065f9d55a 135 n_speed-=0.001;
Picmon 1:0cb065f9d55a 136 setSpeed(&n_speed, mc_Tx_Buffer);
Picmon 1:0cb065f9d55a 137 }
Picmon 1:0cb065f9d55a 138 else
Picmon 1:0cb065f9d55a 139 if(d5_button){//stop
Picmon 1:0cb065f9d55a 140 stopMot(STOP_NO_RAMP, NOT_USED, mc_Tx_Buffer);
Picmon 1:0cb065f9d55a 141 }
Picmon 1:0cb065f9d55a 142 else
Picmon 1:0cb065f9d55a 143 if(d6_button){
Picmon 1:0cb065f9d55a 144
Picmon 1:0cb065f9d55a 145
Picmon 1:0cb065f9d55a 146 }
Picmon 1:0cb065f9d55a 147 else
Picmon 1:0cb065f9d55a 148 if(d7_button){
Picmon 1:0cb065f9d55a 149
Picmon 1:0cb065f9d55a 150
Picmon 1:0cb065f9d55a 151 }
Picmon 1:0cb065f9d55a 152 else
Picmon 1:0cb065f9d55a 153 if(d3_button){
Picmon 1:0cb065f9d55a 154
Picmon 1:0cb065f9d55a 155
Picmon 1:0cb065f9d55a 156 }
Picmon 1:0cb065f9d55a 157 else
Picmon 1:0cb065f9d55a 158 if(d8_button){
Picmon 1:0cb065f9d55a 159
Picmon 1:0cb065f9d55a 160
Picmon 1:0cb065f9d55a 161 }
Picmon 1:0cb065f9d55a 162 else
Picmon 1:0cb065f9d55a 163 if(d9_button){
Picmon 1:0cb065f9d55a 164
Picmon 1:0cb065f9d55a 165
Picmon 1:0cb065f9d55a 166 }
Picmon 1:0cb065f9d55a 167 else
Picmon 1:0cb065f9d55a 168 if(d10_button){
Picmon 1:0cb065f9d55a 169
Picmon 1:0cb065f9d55a 170
Picmon 1:0cb065f9d55a 171 }
Picmon 1:0cb065f9d55a 172 else
Picmon 1:0cb065f9d55a 173 if(d11_button){
Picmon 1:0cb065f9d55a 174
Picmon 0:333434a8611b 175
Picmon 1:0cb065f9d55a 176 }
Picmon 1:0cb065f9d55a 177 else
Picmon 1:0cb065f9d55a 178 if(d12_button){
Picmon 1:0cb065f9d55a 179
Picmon 0:333434a8611b 180
Picmon 1:0cb065f9d55a 181 }
Picmon 1:0cb065f9d55a 182 else
Picmon 1:0cb065f9d55a 183 t1.reset();
Picmon 1:0cb065f9d55a 184 }
Picmon 1:0cb065f9d55a 185
Picmon 1:0cb065f9d55a 186 //mc_debug.printf("cmdCnt = %d", cmdCnt);
Picmon 1:0cb065f9d55a 187 /*
Picmon 1:0cb065f9d55a 188 switch(cmdCnt){
Picmon 1:0cb065f9d55a 189 case 0: setRotMotor(CCW, mc_Tx_Buffer);break;//free run the motor CCW
Picmon 1:0cb065f9d55a 190 case 1: setSpeed(123456, mc_Tx_Buffer);break;//123.456 RPM
Picmon 1:0cb065f9d55a 191 case 2: setRotMotor(CW, mc_Tx_Buffer);break;//free run the motor CW
Picmon 1:0cb065f9d55a 192 case 3: setTorque(3456, mc_Tx_Buffer);break;//3.456N.m
Picmon 1:0cb065f9d55a 193 case 4: setTorque(7000, mc_Tx_Buffer);break;//7N.m
Picmon 1:0cb065f9d55a 194 case 5: set_mL(CW, 1000, mc_Tx_Buffer);break;//dispence 1000ml running CW
Picmon 1:0cb065f9d55a 195 case 6: set_mL(CCW, 1000, mc_Tx_Buffer);break;//dispence 1000ml running CW
Picmon 1:0cb065f9d55a 196 }
Picmon 1:0cb065f9d55a 197 */
Picmon 1:0cb065f9d55a 198 /*
Picmon 1:0cb065f9d55a 199 switch(cmdCnt){
Picmon 1:0cb065f9d55a 200 case READ_DIG_IN: readDigInput(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 201 case READ_DIG_OUT: readDigOutput(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 202 case READ_CURRENT: readCurrent(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 203 case READ_DRV_REG: readDrvReg(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 204 case READ_DRV_REG_EXT: readDrvRegExt(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 205 case READ_DRV_TEMP: readTemp(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 206 case READ_DRV_VOLTAGE: readVoltage(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 207 case READ_DRV_WORKING_SET: readDrvWorkSet(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 208 case READ_DRV_WORKING_SET_EXT: readDrvWorkSetExt(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 209 case READ_ERROR_REG: readErrReg(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 210 case READ_FB_BOOST_CUR: readFBboostCur(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 211 case READ_FB_STATUS: readFBstat(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 212 case READ_FW_VERSION: readFWVwersion(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 213 case READ_FW_CHECKSUM: readFWChecksum(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 214 case READ_MASTER_REG: readMastReg(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 215 case READ_MIN_CURRENT: readMinCur(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 216 case READ_MAX_CURRENT: readMaxCur(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 217 case READ_BOOST_CURRENT: readBoostCur(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 218 case READ_NOM_CURRENT: readNomCur(mc_Tx_Buffer);break;
Picmon 1:0cb065f9d55a 219 }
Picmon 1:0cb065f9d55a 220 */
Picmon 1:0cb065f9d55a 221 /*
Picmon 1:0cb065f9d55a 222 if(rxMsgPending == false){
Picmon 1:0cb065f9d55a 223 rxMsgPending = true;
Picmon 1:0cb065f9d55a 224
Picmon 1:0cb065f9d55a 225 if(cmdCnt < COMMANDS_TO_TEST)
Picmon 1:0cb065f9d55a 226 cmdCnt++;
Picmon 1:0cb065f9d55a 227 else
Picmon 1:0cb065f9d55a 228 cmdCnt=0;
Picmon 1:0cb065f9d55a 229 }
Picmon 1:0cb065f9d55a 230 */
Picmon 1:0cb065f9d55a 231
Picmon 1:0cb065f9d55a 232 //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.
Picmon 1:0cb065f9d55a 233
Picmon 1:0cb065f9d55a 234
Picmon 1:0cb065f9d55a 235 if(getMotMsg(mc_Tx_Buffer, rxMsgStore) == REBOOT){
Picmon 1:0cb065f9d55a 236 mc_usart.printf("\r\n\r\nRebooting the system\r\n\r\n");
Picmon 1:0cb065f9d55a 237 NVIC_SystemReset();
Picmon 1:0cb065f9d55a 238 }
Picmon 1:0cb065f9d55a 239
Picmon 1:0cb065f9d55a 240
Picmon 1:0cb065f9d55a 241 }//2
Picmon 1:0cb065f9d55a 242 }//1