PDA

View Full Version : Vizard and Matlab


sagar29
11-26-2016, 02:12 PM
Hi, I am trying to send a packet from matlab simulink model to vizard using UDP protocol.

Vizard code is:

import viz
import viznet

viz.go()

myNetwork=viz.addNetwork('192.168.1.2')


def onNetwork(e):
if isinstance(e,viz.RawNetworkEvent):
print repr(e.raw_data)

viz.callback(viz.NETWORK_EVENT,onNetwork)




and from simulink I am sending value 7 on port 4950. I am getting output like this:

'\x00\x00\x00\x00\x00\x00\x1c@'
'\x00\x00\x00\x00\x00\x00\x1c@'
'\x00\x00\x00\x00\x00\x00\x1c@'
'\x00\x00\x00\x00\x00\x00\x1c@'
'\x00\x00\x00\x00\x00\x00\x1c@'
'\x00\x00\x00\x00\x00\x00\x1c@'

What is problem exactly!
Thanks

Jeff
11-27-2016, 04:42 PM
What is printed out when you run the following?

import viz
import viznet

viz.go()

myNetwork=viz.addNetwork('192.168.1.2')

def onNetwork(e):
if isinstance(e,viz.RawNetworkEvent):
print 'data', e.raw_data
print 'address', e.address
print 'port', e.port

viz.callback(viz.NETWORK_EVENT,onNetwork)

sagar29
12-01-2016, 03:26 PM
Hi, Thanks for reply.

When i run the code given above, I get output something like this:

data data data
data 50
data data address 192.168.1.2
port 4950
data data data 4950
data 950
data data 2
port 4950
data 950
data 4950
data 950
data 192.168.1.2
port 4950
data 950
data data data 950
data 950
data 4950
data 950
data 950
data port 4950
data 950
data 950
data 4950
data data address 192.168.1.2
port 4950
data 950
data 950
data 0
data data data 192.168.1.2
port 4950
data 950
data address 192.168.1.2
port 4950
data 950
data 0
data 950
data data 950
data 192.168.1.2
port 4950
data data 950
data 192.168.1.2
port 4950
data 950
data 950
data data data 4950
data data 950
data port 4950
data 50
data 4950
data s 192.168.1.2
port 4950
data port 4950
data 950
data 0
data port 4950
data 950
data
data
data 950
data 0
data
port 4950
data 50
data 192.168.1.2
port 4950
data 950
data 192.168.1.2
port 4950
data 192.168.1.2
port 4950
data 192.168.1.2

Is this due to MATLAB is sending faster and Vizard is receiving slower? (i.e. frame sampling problem)

I am sending around 8 to 9 parameters but in vizard only one parameter is reveiving.
In my case; I am trying to give yaw,pitch , roll and other parameters from the matlab and controlling virtual environment.

If this approach of taking data is wrong, please suggest another!

Thanks

Jeff
12-06-2016, 03:18 PM
When receiving raw byte data in Vizard, you'll need to know how the data is stored in order to reconstruct it. Do you have documentation on how the data is sent?

sagar29
12-09-2016, 04:26 PM
Hello,
I amtrying to send 5 bytes from matlab. MATLAb is first taking 6 bytes, creating packet and sending it over UDP. To verify whether MATLAB is sending data or not, I ran MATLAB receiver on vizard PC and and I was able to get proper data.

When I am running this code:
import viz
import viznet

viz.go()

myNetwork=viz.addNetwork('192.168.1.2')

def onNetwork(e):
if isinstance(e,viz.RawNetworkEvent):
print 'data', e.raw_data
print 'address', e.address
print 'port', e.port

viz.callback(viz.NETWORK_EVENT,onNetwork)

I am getting
data #############p@#####@f@#####@ @######i@address 192.168.1.2port 4950data ##############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data ##############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############0q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############Hq@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############`q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############xq@#####@f@#####@ @######i@address 192.168.1.2port 4950data ############# q@#####@f@#####@ @######i@ address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@

and after running print e.raw_data:
I am getting random numbers. In this case, port and IP I am getting properly but problem is with data.

sagar29
12-09-2016, 05:18 PM
In more detail, we are sending data as packets of doubles in Little endian format.

As an alternative, instead of using inbuilt networking commands in vizard, should I write UDP client in python and run?

sagar29
01-07-2017, 07:10 PM
Hello,
I am trying to send data as packets of doubles in little endian format from MATLAB(Simulink) to Vizard. Since MATLAB is non-python interface, I am using code give on the forum and it is:

from __future__ import print_function
import viz
viz.go()
import vizact
import socket

log=open("new.txt","w")
print("Hi there",file=log)

#The maximum amount of data to receive at a time
MAX_DATA_SIZE = 1024

#The port to send/receive data on
PORT = 5600

#Get the name of this computer
COMPUTER_NAME = socket.gethostname()

#Get the IP address of this computer
COMPUTER_IP_ADDRESS = socket.gethostbyname('localhost')

#Create a socket to send data over
#OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

#Create a socket to receive data from
InSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
InSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
InSocket.bind(('192.168.1.2', PORT))
InSocket.setblocking(0)

#def SendData(data):
#OutSocket.sendto(data,(COMPUTER_IP_ADDRESS,PORT))

def ReceiveData():
try:
return InSocket.recv(MAX_DATA_SIZE)
except socket.error:
#Insert error handling code here
print("Error has occured",file=log)

#vizact.onkeydown(' ',SendData,'hello there')

def CheckSocket():
#Try to receive data from socket
data = ReceiveData()
if data:
print (data,file=log)
else:
print("Data not received",file=log)
vizact.ontimer(0,CheckSocket)

I have removed data sending part by making it comment and trying to get data in file so that I can analyse(One more reason is that when I write simple print, I am getting nothing in Vizard). I am not understanding second line in socket creation for receiving data and also not getting any output.

I tried to send data from MATLAB to MATLAB and its working but with Vizard it is giving problem.

Please suggest appropriate corrections in code.

Thanks

monkeyye
12-25-2017, 10:36 PM
Hello,
I amtrying to send 5 bytes from matlab. MATLAb is first taking 6 bytes, creating packet and sending it over UDP. To verify whether MATLAB is sending data or not, I ran MATLAB receiver on vizard PC and and I was able to get proper data.

When I am running this code:
import viz
import viznet

viz.go()

myNetwork=viz.addNetwork('192.168.1.2')

def onNetwork(e):
if isinstance(e,viz.RawNetworkEvent):
print 'data', e.raw_data
print 'address', e.address
print 'port', e.port

viz.callback(viz.NETWORK_EVENT,onNetwork)

I am getting
data #############p@#####@f@#####@ @######i@address 192.168.1.2port 4950data ##############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data ##############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############0q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############Hq@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############`q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############xq@#####@f@#####@ @######i@address 192.168.1.2port 4950data ############# q@#####@f@#####@ @######i@ address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@address 192.168.1.2port 4950data #############q@#####@f@#####@ @######i@

and after running print e.raw_data:
I am getting random numbers. In this case, port and IP I am getting properly but problem is with data.

Hi, there, have you solve this problem?