![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Control robot gripper through modbus
Dependencies: Modbus_For_F446RE mbed
main.cpp@0:20ab14f5bf60, 2017-07-06 (annotated)
- Committer:
- stanley1228
- Date:
- Thu Jul 06 15:17:42 2017 +0000
- Revision:
- 0:20ab14f5bf60
Test for gripper
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stanley1228 | 0:20ab14f5bf60 | 1 | /* |
stanley1228 | 0:20ab14f5bf60 | 2 | * FreeModbus Libary: BARE Demo Application |
stanley1228 | 0:20ab14f5bf60 | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
stanley1228 | 0:20ab14f5bf60 | 4 | * |
stanley1228 | 0:20ab14f5bf60 | 5 | * This program is free software; you can redistribute it and/or modify |
stanley1228 | 0:20ab14f5bf60 | 6 | * it under the terms of the GNU General Public License as published by |
stanley1228 | 0:20ab14f5bf60 | 7 | * the Free Software Foundation; either version 2 of the License, or |
stanley1228 | 0:20ab14f5bf60 | 8 | * (at your option) any later version. |
stanley1228 | 0:20ab14f5bf60 | 9 | * |
stanley1228 | 0:20ab14f5bf60 | 10 | * This program is distributed in the hope that it will be useful, |
stanley1228 | 0:20ab14f5bf60 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
stanley1228 | 0:20ab14f5bf60 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
stanley1228 | 0:20ab14f5bf60 | 13 | * GNU General Public License for more details. |
stanley1228 | 0:20ab14f5bf60 | 14 | * |
stanley1228 | 0:20ab14f5bf60 | 15 | * You should have received a copy of the GNU General Public License |
stanley1228 | 0:20ab14f5bf60 | 16 | * along with this program; if not, write to the Free Software |
stanley1228 | 0:20ab14f5bf60 | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
stanley1228 | 0:20ab14f5bf60 | 18 | * |
stanley1228 | 0:20ab14f5bf60 | 19 | * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ |
stanley1228 | 0:20ab14f5bf60 | 20 | */ |
stanley1228 | 0:20ab14f5bf60 | 21 | |
stanley1228 | 0:20ab14f5bf60 | 22 | /* ----------------------- System includes --------------------------------*/ |
stanley1228 | 0:20ab14f5bf60 | 23 | |
stanley1228 | 0:20ab14f5bf60 | 24 | /* ----------------------- Modbus includes ----------------------------------*/ |
stanley1228 | 0:20ab14f5bf60 | 25 | #include "mb.h" |
stanley1228 | 0:20ab14f5bf60 | 26 | #include "mbport.h" |
stanley1228 | 0:20ab14f5bf60 | 27 | #include "mbed.h" //stanley |
stanley1228 | 0:20ab14f5bf60 | 28 | DigitalOut myled(LED1); //stanley |
stanley1228 | 0:20ab14f5bf60 | 29 | |
stanley1228 | 0:20ab14f5bf60 | 30 | |
stanley1228 | 0:20ab14f5bf60 | 31 | Serial pc(USBTX, USBRX); //stanley |
stanley1228 | 0:20ab14f5bf60 | 32 | |
stanley1228 | 0:20ab14f5bf60 | 33 | /* ----------------------- Defines ------------------------------------------*/ |
stanley1228 | 0:20ab14f5bf60 | 34 | #define REG_INPUT_START (3001) //not use now stanley |
stanley1228 | 0:20ab14f5bf60 | 35 | #define REG_INPUT_NREGS 15 //not use now stanley |
stanley1228 | 0:20ab14f5bf60 | 36 | #define REG_HOLDING_START (4001) |
stanley1228 | 0:20ab14f5bf60 | 37 | #define REG_HOLDING_NREGS 10 |
stanley1228 | 0:20ab14f5bf60 | 38 | |
stanley1228 | 0:20ab14f5bf60 | 39 | #define SLAVE_ID 0x01 |
stanley1228 | 0:20ab14f5bf60 | 40 | |
stanley1228 | 0:20ab14f5bf60 | 41 | //== |
stanley1228 | 0:20ab14f5bf60 | 42 | //Modbus struct |
stanley1228 | 0:20ab14f5bf60 | 43 | //== |
stanley1228 | 0:20ab14f5bf60 | 44 | |
stanley1228 | 0:20ab14f5bf60 | 45 | |
stanley1228 | 0:20ab14f5bf60 | 46 | enum{ |
stanley1228 | 0:20ab14f5bf60 | 47 | DEF_INX_R_GRIPPER_HOLD=0, |
stanley1228 | 0:20ab14f5bf60 | 48 | DEF_INX_L_GRIPPER_HOLD, |
stanley1228 | 0:20ab14f5bf60 | 49 | DEF_INX_TEST1, |
stanley1228 | 0:20ab14f5bf60 | 50 | DEF_INX_TEST2, |
stanley1228 | 0:20ab14f5bf60 | 51 | DEF_INX_CYCLE_COUNT |
stanley1228 | 0:20ab14f5bf60 | 52 | }; |
stanley1228 | 0:20ab14f5bf60 | 53 | |
stanley1228 | 0:20ab14f5bf60 | 54 | /* ----------------------- Static variables ---------------------------------*/ |
stanley1228 | 0:20ab14f5bf60 | 55 | static USHORT usRegInputStart = REG_INPUT_START; //not use now stanley |
stanley1228 | 0:20ab14f5bf60 | 56 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; //not use now stanley |
stanley1228 | 0:20ab14f5bf60 | 57 | |
stanley1228 | 0:20ab14f5bf60 | 58 | static USHORT usRegHoldingStart = REG_HOLDING_START; |
stanley1228 | 0:20ab14f5bf60 | 59 | static USHORT usRegHoldingBuf[REG_HOLDING_NREGS]; |
stanley1228 | 0:20ab14f5bf60 | 60 | |
stanley1228 | 0:20ab14f5bf60 | 61 | /* ----------------------- Start implementation -----------------------------*/ |
stanley1228 | 0:20ab14f5bf60 | 62 | |
stanley1228 | 0:20ab14f5bf60 | 63 | int main( void ) |
stanley1228 | 0:20ab14f5bf60 | 64 | { |
stanley1228 | 0:20ab14f5bf60 | 65 | eMBErrorCode eStatus; |
stanley1228 | 0:20ab14f5bf60 | 66 | |
stanley1228 | 0:20ab14f5bf60 | 67 | eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 460800, MB_PAR_NONE ); |
stanley1228 | 0:20ab14f5bf60 | 68 | |
stanley1228 | 0:20ab14f5bf60 | 69 | /* Enable the Modbus Protocol Stack. */ |
stanley1228 | 0:20ab14f5bf60 | 70 | eMBEnable( ); |
stanley1228 | 0:20ab14f5bf60 | 71 | //eStatus = eMBEnable( ); |
stanley1228 | 0:20ab14f5bf60 | 72 | |
stanley1228 | 0:20ab14f5bf60 | 73 | // Initialise some registers |
stanley1228 | 0:20ab14f5bf60 | 74 | usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]=0; |
stanley1228 | 0:20ab14f5bf60 | 75 | usRegHoldingBuf[DEF_INX_L_GRIPPER_HOLD]=0; |
stanley1228 | 0:20ab14f5bf60 | 76 | usRegHoldingBuf[DEF_INX_TEST1]=123; |
stanley1228 | 0:20ab14f5bf60 | 77 | usRegHoldingBuf[DEF_INX_TEST2]=456; |
stanley1228 | 0:20ab14f5bf60 | 78 | usRegHoldingBuf[DEF_INX_CYCLE_COUNT]=0; |
stanley1228 | 0:20ab14f5bf60 | 79 | |
stanley1228 | 0:20ab14f5bf60 | 80 | |
stanley1228 | 0:20ab14f5bf60 | 81 | myled=1;//stanley |
stanley1228 | 0:20ab14f5bf60 | 82 | |
stanley1228 | 0:20ab14f5bf60 | 83 | while(1) |
stanley1228 | 0:20ab14f5bf60 | 84 | { |
stanley1228 | 0:20ab14f5bf60 | 85 | //(void)eMBPoll( ); origianl |
stanley1228 | 0:20ab14f5bf60 | 86 | // |
stanley1228 | 0:20ab14f5bf60 | 87 | eStatus=eMBPoll(); |
stanley1228 | 0:20ab14f5bf60 | 88 | |
stanley1228 | 0:20ab14f5bf60 | 89 | /* Here we simply count the number of poll cycles. */ |
stanley1228 | 0:20ab14f5bf60 | 90 | usRegHoldingBuf[DEF_INX_CYCLE_COUNT]++; |
stanley1228 | 0:20ab14f5bf60 | 91 | |
stanley1228 | 0:20ab14f5bf60 | 92 | wait_ms(5); //stanley |
stanley1228 | 0:20ab14f5bf60 | 93 | |
stanley1228 | 0:20ab14f5bf60 | 94 | |
stanley1228 | 0:20ab14f5bf60 | 95 | if(usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]==0) |
stanley1228 | 0:20ab14f5bf60 | 96 | { |
stanley1228 | 0:20ab14f5bf60 | 97 | myled=0; |
stanley1228 | 0:20ab14f5bf60 | 98 | } |
stanley1228 | 0:20ab14f5bf60 | 99 | else if(usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]==1) |
stanley1228 | 0:20ab14f5bf60 | 100 | { |
stanley1228 | 0:20ab14f5bf60 | 101 | myled=1; |
stanley1228 | 0:20ab14f5bf60 | 102 | } |
stanley1228 | 0:20ab14f5bf60 | 103 | |
stanley1228 | 0:20ab14f5bf60 | 104 | } |
stanley1228 | 0:20ab14f5bf60 | 105 | } |
stanley1228 | 0:20ab14f5bf60 | 106 | |
stanley1228 | 0:20ab14f5bf60 | 107 | eMBErrorCode |
stanley1228 | 0:20ab14f5bf60 | 108 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
stanley1228 | 0:20ab14f5bf60 | 109 | { |
stanley1228 | 0:20ab14f5bf60 | 110 | eMBErrorCode eStatus = MB_ENOERR; |
stanley1228 | 0:20ab14f5bf60 | 111 | int iRegIndex; |
stanley1228 | 0:20ab14f5bf60 | 112 | |
stanley1228 | 0:20ab14f5bf60 | 113 | if( ( usAddress >= REG_INPUT_START ) |
stanley1228 | 0:20ab14f5bf60 | 114 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
stanley1228 | 0:20ab14f5bf60 | 115 | { |
stanley1228 | 0:20ab14f5bf60 | 116 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
stanley1228 | 0:20ab14f5bf60 | 117 | while( usNRegs > 0 ) |
stanley1228 | 0:20ab14f5bf60 | 118 | { |
stanley1228 | 0:20ab14f5bf60 | 119 | *pucRegBuffer++ = |
stanley1228 | 0:20ab14f5bf60 | 120 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
stanley1228 | 0:20ab14f5bf60 | 121 | *pucRegBuffer++ = |
stanley1228 | 0:20ab14f5bf60 | 122 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
stanley1228 | 0:20ab14f5bf60 | 123 | iRegIndex++; |
stanley1228 | 0:20ab14f5bf60 | 124 | usNRegs--; |
stanley1228 | 0:20ab14f5bf60 | 125 | } |
stanley1228 | 0:20ab14f5bf60 | 126 | } |
stanley1228 | 0:20ab14f5bf60 | 127 | else |
stanley1228 | 0:20ab14f5bf60 | 128 | { |
stanley1228 | 0:20ab14f5bf60 | 129 | eStatus = MB_ENOREG; |
stanley1228 | 0:20ab14f5bf60 | 130 | } |
stanley1228 | 0:20ab14f5bf60 | 131 | |
stanley1228 | 0:20ab14f5bf60 | 132 | return eStatus; |
stanley1228 | 0:20ab14f5bf60 | 133 | } |
stanley1228 | 0:20ab14f5bf60 | 134 | |
stanley1228 | 0:20ab14f5bf60 | 135 | eMBErrorCode |
stanley1228 | 0:20ab14f5bf60 | 136 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) //change variable to REG_HOLDING ,original is all input register |
stanley1228 | 0:20ab14f5bf60 | 137 | { |
stanley1228 | 0:20ab14f5bf60 | 138 | eMBErrorCode eStatus = MB_ENOERR; |
stanley1228 | 0:20ab14f5bf60 | 139 | int iRegIndex; |
stanley1228 | 0:20ab14f5bf60 | 140 | |
stanley1228 | 0:20ab14f5bf60 | 141 | if (eMode == MB_REG_READ) |
stanley1228 | 0:20ab14f5bf60 | 142 | { |
stanley1228 | 0:20ab14f5bf60 | 143 | if( ( usAddress >= REG_HOLDING_START ) |
stanley1228 | 0:20ab14f5bf60 | 144 | && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) |
stanley1228 | 0:20ab14f5bf60 | 145 | { |
stanley1228 | 0:20ab14f5bf60 | 146 | iRegIndex = ( int )( usAddress - usRegHoldingStart ); |
stanley1228 | 0:20ab14f5bf60 | 147 | while( usNRegs > 0 ) |
stanley1228 | 0:20ab14f5bf60 | 148 | { |
stanley1228 | 0:20ab14f5bf60 | 149 | *pucRegBuffer++ = |
stanley1228 | 0:20ab14f5bf60 | 150 | ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 ); |
stanley1228 | 0:20ab14f5bf60 | 151 | *pucRegBuffer++ = |
stanley1228 | 0:20ab14f5bf60 | 152 | ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF ); |
stanley1228 | 0:20ab14f5bf60 | 153 | iRegIndex++; |
stanley1228 | 0:20ab14f5bf60 | 154 | usNRegs--; |
stanley1228 | 0:20ab14f5bf60 | 155 | } |
stanley1228 | 0:20ab14f5bf60 | 156 | } |
stanley1228 | 0:20ab14f5bf60 | 157 | } |
stanley1228 | 0:20ab14f5bf60 | 158 | |
stanley1228 | 0:20ab14f5bf60 | 159 | if (eMode == MB_REG_WRITE) |
stanley1228 | 0:20ab14f5bf60 | 160 | { |
stanley1228 | 0:20ab14f5bf60 | 161 | if( ( usAddress >= REG_HOLDING_START ) |
stanley1228 | 0:20ab14f5bf60 | 162 | && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) |
stanley1228 | 0:20ab14f5bf60 | 163 | { |
stanley1228 | 0:20ab14f5bf60 | 164 | iRegIndex = ( int )( usAddress - usRegHoldingStart ); |
stanley1228 | 0:20ab14f5bf60 | 165 | while( usNRegs > 0 ) |
stanley1228 | 0:20ab14f5bf60 | 166 | { |
stanley1228 | 0:20ab14f5bf60 | 167 | usRegHoldingBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
stanley1228 | 0:20ab14f5bf60 | 168 | pucRegBuffer+=2; |
stanley1228 | 0:20ab14f5bf60 | 169 | iRegIndex++; |
stanley1228 | 0:20ab14f5bf60 | 170 | usNRegs--; |
stanley1228 | 0:20ab14f5bf60 | 171 | } |
stanley1228 | 0:20ab14f5bf60 | 172 | } |
stanley1228 | 0:20ab14f5bf60 | 173 | } |
stanley1228 | 0:20ab14f5bf60 | 174 | |
stanley1228 | 0:20ab14f5bf60 | 175 | return eStatus; |
stanley1228 | 0:20ab14f5bf60 | 176 | } |
stanley1228 | 0:20ab14f5bf60 | 177 | |
stanley1228 | 0:20ab14f5bf60 | 178 | |
stanley1228 | 0:20ab14f5bf60 | 179 | eMBErrorCode |
stanley1228 | 0:20ab14f5bf60 | 180 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
stanley1228 | 0:20ab14f5bf60 | 181 | eMBRegisterMode eMode ) |
stanley1228 | 0:20ab14f5bf60 | 182 | { |
stanley1228 | 0:20ab14f5bf60 | 183 | return MB_ENOREG; |
stanley1228 | 0:20ab14f5bf60 | 184 | } |
stanley1228 | 0:20ab14f5bf60 | 185 | |
stanley1228 | 0:20ab14f5bf60 | 186 | eMBErrorCode |
stanley1228 | 0:20ab14f5bf60 | 187 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
stanley1228 | 0:20ab14f5bf60 | 188 | { |
stanley1228 | 0:20ab14f5bf60 | 189 | return MB_ENOREG; |
stanley1228 | 0:20ab14f5bf60 | 190 | } |
stanley1228 | 0:20ab14f5bf60 | 191 |