Abstract
In this tutorial, we will explore the scripting capabilities of Roboteq drives to interface with a Torque meter that supports the Modbus protocol. Typically, such communication would necessitate a Modbus Master capable of reading and writing the required registers. Although Roboteq controllers cannot natively function as fieldbus master devices, the Raw redirect function enables the construction, sending, and reading of raw RS485 frames transmitted through the RS485 bus. This allows for the implementation of a simplified version of the Modbus protocol to exchange data with the sensor.
Torque Meter
The Torque Meter provides the torque value in Nm with a resolution of two decimal places upon receiving a read request. The following information must be referenced in the Torque Sensor's manual to establish the Modbus protocol and to request the torque meter value:
Parameter | Value | Description |
Baud Rate | 38400 | Roboteq drive and the torque meter must be configured with the same baud rate. The default value for the torque meter’s Baud Rate according to the datasheet is 38400 |
Slave ID | 01H | The default slave ID of the torque meter, which is required to implement the Modbus protocol. |
Torque Parameter Address | 00H | This is the address of the holding register that will return the torque meter value. |
With this information, we can construct the following Raw RS-485 frame to request the torque value:
Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 | Byte 5 | Byte 6 | Byte 7 |
01 | 03 | 00 | 00 | 00 | 00 | C4 | 0B |
The explanation of each byte follows:
Byte 0: The node ID of the slave.
Byte 1: The read Holding Register function according to Modbus protocol.
Byte 2: The torque value Holding register ID
Byte 3: Not used in read requests.
Byte 4: Not used in read requests.
Byte 5: Not used in read requests.
Byte 6: The CRC less significant byte
Byte 7: The CRC most significant byte
The torque meter should reply with a frame having the following format:
Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 | Byte 5 | Byte 6 | Byte 7 | Byte 8 |
01 | 03 | 04 | D1 | D2 | D3 | D4 | CRC0 | CRC1 |
The explanation of each byte follows:
Byte 0: The node ID of the slave.
Byte 1: The read Holding Register function according to Modbus protocol.
Byte 2: The number of bytes that are being returned.
Byte 3 to 6: The bytes of the Torque value.
Byte 7: The CRC less significant byte. Calculated according to the return values.
Byte 8: The CRC most significant byte. Calculated according to the return values.
Hardware Connections
The hardware connections are illustrated in the following diagram. The torque meter requires 24V, so separate power supplies will be used to power the sensor and the drive. These two power supplies must share a common ground. Please note that this connection should not be made through the GND pin of the drive’s D-sub connector, as permanent damage may occur in the event of disconnection of the main battery’s negative lead. Also, be aware that the RS485A pin of the sensor should be connected to the RS485+ pin of the drive, and the RS485B pin of the sensor to the RS485- pin of the drive. The symbols A, B, +, and – are used interchangeably.
Roborun+ Configurations
The next steps should be followed to enable the Raw redirect on the utility:
1. Set the serial port’s baud rate. In our case it is 38400.
2. Enable the Raw redirect to the RS485 port.
3. Set the Modbus mode to RS-485 RTU
4. Set the motor torque constant. This parameter will be used by the drive to calculate the motor torque from the measured motor current. Later, the calculated torque can be compared with the measured torque of the sensor to ensure accuracy and consistency in performance monitoring.
MicroBasic Script
The script will implement a simplified version of the Modbus protocol. It will construct and send the required frame to read the torque data. Upon receiving a reply, the script will read the received frame, extract the data bytes, and convert the float value to an integer so it can be displayed on Roborun. The CRC value of the sent frame is predefined based on the frame data, and it will not be calculated by the script. Additionally, the CRC of the received frame will not be checked. However, the script will perform a basic evaluation of the received data to ensure that the node ID of the slave, the function code, and the number of data bytes are correct; otherwise, the frame will be rejected.
The script can be parameterized by modifying the sensor ID (SENSOR_ID), torque register address (TORQUE_ADDR), and the two variables where the measured torque from the sensor and the calculated value from the Roboteq drive will be stored (SENS_VAR and TRQ_VAR).
For more information about the Raw Redirect implementation refer to the respective article.
The script follows:
'------------------------------------------------------------
' ATO torque sensor Interface script | v.1
'------------------------------------------------------------
'Operation:
'--------------
'Sends a Raw redirect frame to read the sensor's torque data.
'Performs all the necessary convertions and adds the data to
'the required VAR
'------------------------------------------------------------
'Settings:
'--------------
' Sensor's default baud rate: 57600
' Senror's default Id: 0x01
'------------------------------------------------------------
' Instructions:
'--------------
' Enable the raw redirect on Roborun for 485 and
' enable the modbus475 RTU. Set the serial baud rate to match
' the sensor's baud rate
'-------------------------------------------------------------
option explicit
#define SENS_VAR 1 'The number of VAR where the actual torque value wil be stored
#define TRQ_VAR 2 'The number of VAR where the drive's measured torque will be stored
#define SENSOR_ID 0x01
#define TORQUE_ADDR 0x00 'The addres of the Torque register
#define COMMAND_TYPE 0x03 'Read request
'Below is the required frame to send, to read the Torque value
'CRC is set constanlty according to the sent frame as 0xOBC4
'0x01 | 0x03 | 0x00 | 0x00 | 0x00 | 0x02 | 0xC4 | 0x0B
dim inputBuffer[64] as integer ' Readf up to 64 bytes
dim byte[16] as integer ' Send up to 16 bytes
dim incomingFrameLength as integer
dim i as integer
dim torque as integer
'Frame construction
byte[0] = SENSOR_ID
byte[1] = COMMAND_TYPE
byte[2] = TORQUE_ADDR
byte[3] = 0
byte[4] = 0
byte[5] = 2
byte[6] = 0xC4
byte[7] = 0x0B
setconfig(_SCRO,2) 'set the script output to USB for debugging. In case it was set to something else.
top:
gosub Send_MODBUS_RTU_Frame ' Read request
wait(20) ' 50 Hz update rate
gosub Receive_MODBUS_RTU_Frame ' Receive the data
setcommand(_VAR,SENS_VAR,torque) 'First var will have the torque sensor value
setcommand(_VAR,TRQ_VAR,-getvalue(_TRQ,1)*707/1000) 'Second var will have the drive's measured torque value, converted to rms
goto top
Send_MODBUS_RTU_Frame:
' Send the data with CU function
setcommand(_CU, 3, byte[0])
setcommand(_CU, 4, byte[1])
setcommand(_CU, 5, byte[2])
setcommand(_CU, 6, byte[3])
setcommand(_CU, 7, byte[4])
setcommand(_CU, 8, byte[5])
setcommand(_CU, 9, byte[6])
setcommand(_CU, 10, byte[7])
' Data will be automatically sent after The frame length is set
setcommand(_CU, 1, 6) ' for RS485
setcommand(_CU, 2, 0x08) ' set frame length and Tx frame
return
Receive_MODBUS_RTU_Frame:
if (getValue(_CD,1)) ' CD returns the number of bytes awaiting on the input buffer and copies them to the read buffer.
incomingFrameLength = getValue(_DDT,1)' get the frame size
for i = 0 andwhile i < incomingFrameLength
inputBuffer[i] = getValue(_DDT,i+2) 'update the input buffer with the data bytes
next
if ((inputBuffer[2] = 0x04) and (inputBuffer[1] = COMMAND_TYPE and (inputBuffer[0] = SENSOR_ID)) 'A very basic test to confirm the details of the sender
torque = inputBuffer[3]<<24 or inputBuffer[4]<<16 or inputBuffer[5]<<8 or inputBuffer[6] 'shift the bytes to add them to the same torque variable
' print(torque) 'Torque value for debugging
' print("\n")
end if
end if
return