1.Combine library into this project 2.Use this to do the complete fuction
Dependencies: DXL_SDK_For_F446RE Matrix Modbus_For_F446RE RobotControl_7Axis mbed
main.cpp@1:249f89a36223, 2017-03-31 (annotated)
- Committer:
- stanley1228
- Date:
- Fri Mar 31 10:39:39 2017 +0800
- Revision:
- 1:249f89a36223
- Parent:
- 0:f147c77caac0
- Child:
- 4:8fc956bd0e78
1.change mbpoll to ticker.interval=10ms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stanley1228 | 0:f147c77caac0 | 1 | #include "mb.h" |
stanley1228 | 0:f147c77caac0 | 2 | #include "mbport.h" |
stanley1228 | 0:f147c77caac0 | 3 | #include "mbed.h" //stanley |
stanley1228 | 0:f147c77caac0 | 4 | #include "dynamixel.h" |
stanley1228 | 0:f147c77caac0 | 5 | |
stanley1228 | 0:f147c77caac0 | 6 | #define DEBUG 1 |
stanley1228 | 0:f147c77caac0 | 7 | |
stanley1228 | 0:f147c77caac0 | 8 | #if (DEBUG) |
stanley1228 | 0:f147c77caac0 | 9 | #define DBGMSG(x) pc.printf x; |
stanley1228 | 0:f147c77caac0 | 10 | #else |
stanley1228 | 0:f147c77caac0 | 11 | #define DBGMSG(x) |
stanley1228 | 0:f147c77caac0 | 12 | #endif |
stanley1228 | 0:f147c77caac0 | 13 | |
stanley1228 | 0:f147c77caac0 | 14 | |
stanley1228 | 0:f147c77caac0 | 15 | DigitalOut myled(LED1); //stanley |
stanley1228 | 0:f147c77caac0 | 16 | |
stanley1228 | 0:f147c77caac0 | 17 | |
stanley1228 | 0:f147c77caac0 | 18 | Serial pc(USBTX, USBRX); //stanley |
stanley1228 | 0:f147c77caac0 | 19 | |
stanley1228 | 0:f147c77caac0 | 20 | /* ----------------------- Defines ------------------------------------------*/ |
stanley1228 | 0:f147c77caac0 | 21 | #define REG_INPUT_START (3001) //not use now stanley |
stanley1228 | 0:f147c77caac0 | 22 | #define REG_INPUT_NREGS 15 //not use now stanley |
stanley1228 | 0:f147c77caac0 | 23 | #define REG_HOLDING_START (4001) |
stanley1228 | 0:f147c77caac0 | 24 | #define REG_HOLDING_NREGS 28 |
stanley1228 | 0:f147c77caac0 | 25 | |
stanley1228 | 0:f147c77caac0 | 26 | #define SLAVE_ID 0x0A |
stanley1228 | 0:f147c77caac0 | 27 | #define DEF_MAX_AXIS 7 |
stanley1228 | 0:f147c77caac0 | 28 | //== |
stanley1228 | 0:f147c77caac0 | 29 | //Modbus struct |
stanley1228 | 0:f147c77caac0 | 30 | //== |
stanley1228 | 0:f147c77caac0 | 31 | //Input register 0~14 |
stanley1228 | 0:f147c77caac0 | 32 | //public UInt16[] PosVal = new UInt16[DEF_MAX_AXIS]; 7 |
stanley1228 | 0:f147c77caac0 | 33 | //public Int16[] VelValue = new Int16[DEF_MAX_AXIS]; 7 |
stanley1228 | 0:f147c77caac0 | 34 | //public Int16 Err_State = 0; 1 |
stanley1228 | 0:f147c77caac0 | 35 | |
stanley1228 | 0:f147c77caac0 | 36 | ////Holding register 0~5 |
stanley1228 | 0:f147c77caac0 | 37 | //public Int16 TargetPosX; 1 |
stanley1228 | 0:f147c77caac0 | 38 | //public Int16 TargetPosY; 1 |
stanley1228 | 0:f147c77caac0 | 39 | //public Int16 TargetPosZ; 1 |
stanley1228 | 0:f147c77caac0 | 40 | //public Int16 OPMode; //P2P 1 |
stanley1228 | 0:f147c77caac0 | 41 | //public Int16 SpeedRatio; //0~1 1 |
stanley1228 | 0:f147c77caac0 | 42 | |
stanley1228 | 0:f147c77caac0 | 43 | enum{ |
stanley1228 | 0:f147c77caac0 | 44 | DEF_INX_TARGET_POSX=0, |
stanley1228 | 0:f147c77caac0 | 45 | DEF_INX_TARGET_POSY, |
stanley1228 | 0:f147c77caac0 | 46 | DEF_INX_TARGET_POSZ, |
stanley1228 | 0:f147c77caac0 | 47 | DEF_INX_OPMODE, |
stanley1228 | 0:f147c77caac0 | 48 | DEF_INX_SPPED_RATIO_L, |
stanley1228 | 0:f147c77caac0 | 49 | DEF_INX_SPPED_RATIO_H, |
stanley1228 | 0:f147c77caac0 | 50 | |
stanley1228 | 0:f147c77caac0 | 51 | DEF_INX_TARPOS1=6, |
stanley1228 | 0:f147c77caac0 | 52 | DEF_INX_TARPOS2, |
stanley1228 | 0:f147c77caac0 | 53 | DEF_INX_TARPOS3, |
stanley1228 | 0:f147c77caac0 | 54 | DEF_INX_TARPOS4, |
stanley1228 | 0:f147c77caac0 | 55 | DEF_INX_TARPOS5, |
stanley1228 | 0:f147c77caac0 | 56 | DEF_INX_TARPOS6, |
stanley1228 | 0:f147c77caac0 | 57 | DEF_INX_TARPOS7, |
stanley1228 | 0:f147c77caac0 | 58 | |
stanley1228 | 0:f147c77caac0 | 59 | DEF_INX_POSVAL1=13, |
stanley1228 | 0:f147c77caac0 | 60 | DEF_INX_POSVAL2, |
stanley1228 | 0:f147c77caac0 | 61 | DEF_INX_POSVAL3, |
stanley1228 | 0:f147c77caac0 | 62 | DEF_INX_POSVAL4, |
stanley1228 | 0:f147c77caac0 | 63 | DEF_INX_POSVAL5, |
stanley1228 | 0:f147c77caac0 | 64 | DEF_INX_POSVAL6, |
stanley1228 | 0:f147c77caac0 | 65 | DEF_INX_POSVAL7, |
stanley1228 | 0:f147c77caac0 | 66 | |
stanley1228 | 0:f147c77caac0 | 67 | DEF_INX_VELVAL1=20, |
stanley1228 | 0:f147c77caac0 | 68 | DEF_INX_VELVAL2, |
stanley1228 | 0:f147c77caac0 | 69 | DEF_INX_VELVAL3, |
stanley1228 | 0:f147c77caac0 | 70 | DEF_INX_VELVAL4, |
stanley1228 | 0:f147c77caac0 | 71 | DEF_INX_VELVAL5, |
stanley1228 | 0:f147c77caac0 | 72 | DEF_INX_VELVAL6, |
stanley1228 | 0:f147c77caac0 | 73 | DEF_INX_VELVAL7 |
stanley1228 | 0:f147c77caac0 | 74 | }; |
stanley1228 | 0:f147c77caac0 | 75 | |
stanley1228 | 0:f147c77caac0 | 76 | enum eOPMode |
stanley1228 | 0:f147c77caac0 | 77 | { |
stanley1228 | 0:f147c77caac0 | 78 | JOG, |
stanley1228 | 0:f147c77caac0 | 79 | P2P, |
stanley1228 | 0:f147c77caac0 | 80 | SEWING, |
stanley1228 | 0:f147c77caac0 | 81 | LINE |
stanley1228 | 0:f147c77caac0 | 82 | }; |
stanley1228 | 0:f147c77caac0 | 83 | /* ----------------------- Static variables ---------------------------------*/ |
stanley1228 | 0:f147c77caac0 | 84 | static USHORT usRegInputStart = REG_INPUT_START; //not use now stanley |
stanley1228 | 0:f147c77caac0 | 85 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; //not use now stanley |
stanley1228 | 0:f147c77caac0 | 86 | |
stanley1228 | 0:f147c77caac0 | 87 | static USHORT usRegHoldingStart = REG_HOLDING_START; |
stanley1228 | 0:f147c77caac0 | 88 | static USHORT usRegHoldingBuf[REG_HOLDING_NREGS]; |
stanley1228 | 0:f147c77caac0 | 89 | |
stanley1228 | 0:f147c77caac0 | 90 | /* ----------------------- Start implementation -----------------------------*/ |
stanley1228 | 1:249f89a36223 | 91 | Ticker tMobusPoll; |
stanley1228 | 1:249f89a36223 | 92 | |
stanley1228 | 1:249f89a36223 | 93 | void fMobusPoll() |
stanley1228 | 1:249f89a36223 | 94 | { |
stanley1228 | 1:249f89a36223 | 95 | //(void)eMBPoll( ); origianl |
stanley1228 | 1:249f89a36223 | 96 | // |
stanley1228 | 1:249f89a36223 | 97 | eMBPoll(); |
stanley1228 | 1:249f89a36223 | 98 | |
stanley1228 | 1:249f89a36223 | 99 | /* Here we simply count the number of poll cycles. */ |
stanley1228 | 1:249f89a36223 | 100 | usRegHoldingBuf[DEF_INX_TARGET_POSX]++; |
stanley1228 | 1:249f89a36223 | 101 | |
stanley1228 | 1:249f89a36223 | 102 | if(usRegHoldingBuf[DEF_INX_TARGET_POSX]==200)//stanley |
stanley1228 | 1:249f89a36223 | 103 | { |
stanley1228 | 1:249f89a36223 | 104 | myled=!myled; |
stanley1228 | 1:249f89a36223 | 105 | usRegHoldingBuf[DEF_INX_TARGET_POSX]=0; |
stanley1228 | 1:249f89a36223 | 106 | } |
stanley1228 | 1:249f89a36223 | 107 | } |
stanley1228 | 1:249f89a36223 | 108 | |
stanley1228 | 0:f147c77caac0 | 109 | |
stanley1228 | 0:f147c77caac0 | 110 | int main( void ) |
stanley1228 | 0:f147c77caac0 | 111 | { |
stanley1228 | 0:f147c77caac0 | 112 | eMBErrorCode eStatus; |
stanley1228 | 0:f147c77caac0 | 113 | |
stanley1228 | 0:f147c77caac0 | 114 | eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE ); |
stanley1228 | 0:f147c77caac0 | 115 | |
stanley1228 | 0:f147c77caac0 | 116 | /* Enable the Modbus Protocol Stack. */ |
stanley1228 | 0:f147c77caac0 | 117 | eMBEnable( ); |
stanley1228 | 0:f147c77caac0 | 118 | //eStatus = eMBEnable( ); |
stanley1228 | 0:f147c77caac0 | 119 | |
stanley1228 | 1:249f89a36223 | 120 | |
stanley1228 | 1:249f89a36223 | 121 | |
stanley1228 | 0:f147c77caac0 | 122 | // Initialise some registers |
stanley1228 | 0:f147c77caac0 | 123 | // usRegInputBuf[1] = 0x1234; |
stanley1228 | 0:f147c77caac0 | 124 | // usRegInputBuf[2] = 0x5678; |
stanley1228 | 0:f147c77caac0 | 125 | // usRegInputBuf[3] = 0x9abc; |
stanley1228 | 0:f147c77caac0 | 126 | //usRegInputBuf[4] = 0x1000; |
stanley1228 | 0:f147c77caac0 | 127 | // usRegInputBuf[5] = 0x1001; |
stanley1228 | 0:f147c77caac0 | 128 | // usRegInputBuf[6] = 0x1002; |
stanley1228 | 0:f147c77caac0 | 129 | |
stanley1228 | 0:f147c77caac0 | 130 | usRegHoldingBuf[DEF_INX_TARGET_POSX]=0; |
stanley1228 | 0:f147c77caac0 | 131 | usRegHoldingBuf[DEF_INX_TARGET_POSY]=1000; |
stanley1228 | 0:f147c77caac0 | 132 | usRegHoldingBuf[DEF_INX_TARGET_POSZ]=2000; |
stanley1228 | 0:f147c77caac0 | 133 | usRegHoldingBuf[DEF_INX_OPMODE]=JOG; |
stanley1228 | 0:f147c77caac0 | 134 | |
stanley1228 | 0:f147c77caac0 | 135 | float fSpeedRatio=0.345; |
stanley1228 | 0:f147c77caac0 | 136 | USHORT* usp=(USHORT*)&fSpeedRatio; |
stanley1228 | 0:f147c77caac0 | 137 | usRegHoldingBuf[DEF_INX_SPPED_RATIO_L]=*usp; |
stanley1228 | 0:f147c77caac0 | 138 | usRegHoldingBuf[DEF_INX_SPPED_RATIO_H]=*(usp+1); |
stanley1228 | 0:f147c77caac0 | 139 | |
stanley1228 | 0:f147c77caac0 | 140 | for(int i=0;i<DEF_MAX_AXIS;i++) |
stanley1228 | 0:f147c77caac0 | 141 | { |
stanley1228 | 0:f147c77caac0 | 142 | usRegHoldingBuf[DEF_INX_TARPOS1+i]=500+i; |
stanley1228 | 0:f147c77caac0 | 143 | usRegHoldingBuf[DEF_INX_POSVAL1+i]=1000+i; |
stanley1228 | 0:f147c77caac0 | 144 | usRegHoldingBuf[DEF_INX_VELVAL1+i]=2000+i; |
stanley1228 | 0:f147c77caac0 | 145 | } |
stanley1228 | 0:f147c77caac0 | 146 | |
stanley1228 | 0:f147c77caac0 | 147 | |
stanley1228 | 1:249f89a36223 | 148 | tMobusPoll.attach_us(&fMobusPoll,10000); |
stanley1228 | 0:f147c77caac0 | 149 | myled=1;//stanley |
stanley1228 | 0:f147c77caac0 | 150 | |
stanley1228 | 1:249f89a36223 | 151 | while(1); |
stanley1228 | 1:249f89a36223 | 152 | |
stanley1228 | 0:f147c77caac0 | 153 | } |
stanley1228 | 0:f147c77caac0 | 154 | |
stanley1228 | 0:f147c77caac0 | 155 | eMBErrorCode |
stanley1228 | 0:f147c77caac0 | 156 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
stanley1228 | 0:f147c77caac0 | 157 | { |
stanley1228 | 0:f147c77caac0 | 158 | eMBErrorCode eStatus = MB_ENOERR; |
stanley1228 | 0:f147c77caac0 | 159 | int iRegIndex; |
stanley1228 | 0:f147c77caac0 | 160 | |
stanley1228 | 0:f147c77caac0 | 161 | if( ( usAddress >= REG_INPUT_START ) |
stanley1228 | 0:f147c77caac0 | 162 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
stanley1228 | 0:f147c77caac0 | 163 | { |
stanley1228 | 0:f147c77caac0 | 164 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
stanley1228 | 0:f147c77caac0 | 165 | while( usNRegs > 0 ) |
stanley1228 | 0:f147c77caac0 | 166 | { |
stanley1228 | 0:f147c77caac0 | 167 | *pucRegBuffer++ = |
stanley1228 | 0:f147c77caac0 | 168 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
stanley1228 | 0:f147c77caac0 | 169 | *pucRegBuffer++ = |
stanley1228 | 0:f147c77caac0 | 170 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
stanley1228 | 0:f147c77caac0 | 171 | iRegIndex++; |
stanley1228 | 0:f147c77caac0 | 172 | usNRegs--; |
stanley1228 | 0:f147c77caac0 | 173 | } |
stanley1228 | 0:f147c77caac0 | 174 | } |
stanley1228 | 0:f147c77caac0 | 175 | else |
stanley1228 | 0:f147c77caac0 | 176 | { |
stanley1228 | 0:f147c77caac0 | 177 | eStatus = MB_ENOREG; |
stanley1228 | 0:f147c77caac0 | 178 | } |
stanley1228 | 0:f147c77caac0 | 179 | |
stanley1228 | 0:f147c77caac0 | 180 | return eStatus; |
stanley1228 | 0:f147c77caac0 | 181 | } |
stanley1228 | 0:f147c77caac0 | 182 | |
stanley1228 | 0:f147c77caac0 | 183 | eMBErrorCode |
stanley1228 | 0:f147c77caac0 | 184 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) //change variable to REG_HOLDING ,original is all input register |
stanley1228 | 0:f147c77caac0 | 185 | { |
stanley1228 | 0:f147c77caac0 | 186 | eMBErrorCode eStatus = MB_ENOERR; |
stanley1228 | 0:f147c77caac0 | 187 | int iRegIndex; |
stanley1228 | 0:f147c77caac0 | 188 | |
stanley1228 | 0:f147c77caac0 | 189 | if (eMode == MB_REG_READ) |
stanley1228 | 0:f147c77caac0 | 190 | { |
stanley1228 | 0:f147c77caac0 | 191 | if( ( usAddress >= REG_HOLDING_START ) |
stanley1228 | 0:f147c77caac0 | 192 | && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) |
stanley1228 | 0:f147c77caac0 | 193 | { |
stanley1228 | 0:f147c77caac0 | 194 | iRegIndex = ( int )( usAddress - usRegHoldingStart ); |
stanley1228 | 0:f147c77caac0 | 195 | while( usNRegs > 0 ) |
stanley1228 | 0:f147c77caac0 | 196 | { |
stanley1228 | 0:f147c77caac0 | 197 | *pucRegBuffer++ = |
stanley1228 | 0:f147c77caac0 | 198 | ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 ); |
stanley1228 | 0:f147c77caac0 | 199 | *pucRegBuffer++ = |
stanley1228 | 0:f147c77caac0 | 200 | ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF ); |
stanley1228 | 0:f147c77caac0 | 201 | iRegIndex++; |
stanley1228 | 0:f147c77caac0 | 202 | usNRegs--; |
stanley1228 | 0:f147c77caac0 | 203 | } |
stanley1228 | 0:f147c77caac0 | 204 | } |
stanley1228 | 0:f147c77caac0 | 205 | } |
stanley1228 | 0:f147c77caac0 | 206 | |
stanley1228 | 0:f147c77caac0 | 207 | if (eMode == MB_REG_WRITE) |
stanley1228 | 0:f147c77caac0 | 208 | { |
stanley1228 | 0:f147c77caac0 | 209 | if( ( usAddress >= REG_HOLDING_START ) |
stanley1228 | 0:f147c77caac0 | 210 | && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) |
stanley1228 | 0:f147c77caac0 | 211 | { |
stanley1228 | 0:f147c77caac0 | 212 | iRegIndex = ( int )( usAddress - usRegHoldingStart ); |
stanley1228 | 0:f147c77caac0 | 213 | while( usNRegs > 0 ) |
stanley1228 | 0:f147c77caac0 | 214 | { |
stanley1228 | 0:f147c77caac0 | 215 | usRegHoldingBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
stanley1228 | 0:f147c77caac0 | 216 | pucRegBuffer+=2; |
stanley1228 | 0:f147c77caac0 | 217 | iRegIndex++; |
stanley1228 | 0:f147c77caac0 | 218 | usNRegs--; |
stanley1228 | 0:f147c77caac0 | 219 | } |
stanley1228 | 0:f147c77caac0 | 220 | } |
stanley1228 | 0:f147c77caac0 | 221 | } |
stanley1228 | 0:f147c77caac0 | 222 | |
stanley1228 | 0:f147c77caac0 | 223 | return eStatus; |
stanley1228 | 0:f147c77caac0 | 224 | } |
stanley1228 | 0:f147c77caac0 | 225 | |
stanley1228 | 0:f147c77caac0 | 226 | |
stanley1228 | 0:f147c77caac0 | 227 | eMBErrorCode |
stanley1228 | 0:f147c77caac0 | 228 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
stanley1228 | 0:f147c77caac0 | 229 | eMBRegisterMode eMode ) |
stanley1228 | 0:f147c77caac0 | 230 | { |
stanley1228 | 0:f147c77caac0 | 231 | return MB_ENOREG; |
stanley1228 | 0:f147c77caac0 | 232 | } |
stanley1228 | 0:f147c77caac0 | 233 | |
stanley1228 | 0:f147c77caac0 | 234 | eMBErrorCode |
stanley1228 | 0:f147c77caac0 | 235 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
stanley1228 | 0:f147c77caac0 | 236 | { |
stanley1228 | 0:f147c77caac0 | 237 | return MB_ENOREG; |
stanley1228 | 0:f147c77caac0 | 238 | } |
stanley1228 | 0:f147c77caac0 | 239 |