TCP connection between MG400 and Arduino H7 with Arduine vision shield


#1

Dear

I have an Arduino H7 board with an Arduino Vision Shield and I want to set up a TCP communication with a Dobot MG400 robot to send the position of objects detected by the Arduino Vision shield. I want to use the MG400 as a server and the Arduino H7 with Vision shield as a client. First of all is this possible? Second, does anyone have experience with this type of communication in Arduino and in DobotStudio Pro?

Thanks in advance.

Kind regards,

Frederik


#2

Hi Frederik,

I do not know the Arduino Vision shield but I try to achieve the same with the Huskylens for object recognition and the RP2040 W5100S-EVB-Pico as interface for theHuskylens and as TCP-client for the MG400.
My current state is that the RP2040 reads the X-Y coordinates from the objects detected by the Huskylens and that I can type the Server port 29999 commands, send these to MG400 and recieve the proper respons.
My next step is to find a way to communicate the Huskylens coordinates to the running program in the MG400.
In my search for information about this subject I bumped into your question and if you are interested I can share my CircuitPython script I use for the RP2040.


#3

Dear

Is it possible to share the code of your camera?

Kind regards,

Frederik


#4

Frederik,
Please be aware that I am not a programmer and that until now I have not acchieved my goals but below CircuitPython script runs on the RP2040 W5100S-EVB-Pico, It does read the X-Y coordinates from the object detected by the Huskylens and is able to set-up a TCP connection with the MG400.

import time
import board
import busio
import digitalio
import binascii as ubinascii
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

def cmdToBytes(cmd):
return ubinascii.unhexlify(cmd)

#chunks from list_array with size n.
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]

def bytes_to_u16(first_byte,second_byte):
return ((second_byte & 0xFF) << 8) | (first_byte & 0xFF)

commandHeaderAndAddress = “55AA11”
algorthimsByteID = {
“ALGORITHM_OBJECT_TRACKING”: “0100”,
“ALGORITHM_FACE_RECOGNITION”: “0000”,
“ALGORITHM_OBJECT_RECOGNITION”: “0200”,
“ALGORITHM_LINE_TRACKING”: “0300”,
“ALGORITHM_COLOR_RECOGNITION”: “0400”,
“ALGORITHM_TAG_RECOGNITION”: “0500”,
“ALGORITHM_OBJECT_CLASSIFICATION”: “0600”
}

COMMAND_REQUEST_CUSTOMNAMES= 0x2f
COMMAND_REQUEST_TAKE_PHOTO_TO_SD_CARD = 0x30
COMMAND_REQUEST_SAVE_MODEL_TO_SD_CARD = 0x32
COMMAND_REQUEST_LOAD_MODEL_FROM_SD_CARD = 0x33
COMMAND_REQUEST_CUSTOM_TEXT = 0x34
COMMAND_REQUEST_CLEAR_TEXT = 0x35
COMMAND_REQUEST_LEARN_ONECE = 0x36
COMMAND_REQUEST_FORGET = 0x37
COMMAND_REQUEST_SCREENSHOT_TO_SD_CARD = 0x39
COMMAND_REQUEST_FIRMWARE_VERSION = 0x3C

command_request_knock = cmdToBytes(commandHeaderAndAddress+“002c3c”)
command_request = cmdToBytes(commandHeaderAndAddress+“002030”)
command_request_blocks = cmdToBytes(commandHeaderAndAddress+“002131”)
command_request_arrows = cmdToBytes(commandHeaderAndAddress+“002232”)

cmd = command_request
#---------------------------------------------------------------------
#WIZnet Ethernet Hat on Raspberry Pi Pico [W]
cs = digitalio.DigitalInOut(board.GP17)
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
eth = WIZNET5K(spi, cs, mac=(0xDE, 0xAD, 0xBE, 0xEF, 0x06, 247), is_dhcp=True)

IP_ADDRESS = (192, 168, 2, 10)
SUBNET_MASK = (255, 255, 0, 0)
GATEWAY_ADDRESS = (192, 168, 2, 1)
DNS_SERVER = (8, 8, 8, 8)

#Set network configuration
#eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
pool = requests

CLIENT = eth.pretty_ip(eth.ip_address)
HOST = “192.168.2.6”
PORT = 6601
#PORT = 29999
TIMEOUT = None
MAXBUF = 200
INTERVAL=2

#---------------------------------------------------------------------

#HUSKYLENS

#i2c = busio.I2C(board.GP17, board.GP16, frequency=30000)
i2c= busio.I2C(scl=board.GP5, sda=board.GP4, frequency=30000)

while not i2c.try_lock():
pass

data = bytearray(200) # Read 200 bytes of data

#Read Huslylens command register. Huskylens address is 0x32
i2c.writeto(0x32, cmd )
time.sleep(0.01)
i2c.readfrom_into(0x32, data)

i2c.unlock()

n = 16 # number of elements in the COMMAND array
#chunk = [0x55,0xaa,0x11,0xa,0x29,0xb,0x0,0x1,0x0,0x88,0x12,0x0,0x0,0x0,0x0,0xe9]

chunk = list(divide_chunks(data, n))

i =0 # pointer to loop through the COMMAND array
while chunk[i][0]== 85: #Header of COMMAND response string

if chunk[i][4] == 41: 	# 41 is 0x29 is COMMAND_RETURN_INFO
    print(chunk[i][5], " blokjes")
if chunk[i][4] == 42: 	# 42 is COMMAND_RETURN_BLOCK(0x2A):
    X_pos = bytes_to_u16(chunk[i][5], chunk[i][6])
    Y_pos = bytes_to_u16(chunk[i][7], chunk[i][8])                    
    Width = bytes_to_u16(chunk[i][9], chunk[i][10])
    Height= bytes_to_u16(chunk[i][11], chunk[i][12])
    print("X:", X_pos, "Y:", Y_pos) 
    print("H:", Height,"B:", Width, "\n")        
i=i+1

print(f"WIZnet Client IP Address: {CLIENT}")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT)

while True:
print(“Create TCP Client Socket”)

print("Connecting")
s.connect((HOST, PORT))

dobot_input = input("Enter DobotRobot command:" )

size = s.send(b'{}'.format(dobot_input))
print("Sent", size, "bytes")

buf = s.recv(MAXBUF)
#buf = s.recv()
print('Received', buf)

s.close()

time.sleep(INTERVAL)