PDA

View Full Version : Real-time export data to simulink


whj
08-31-2009, 02:55 PM
Hello,

I'm seeking a solution to output position and orientation data to another computer running Simulink in real time. Does anybody have any experience?

Thanks!

farshizzo
09-03-2009, 04:48 PM
Can Simulink read data coming over a network socket? If so, you can use the Python socket module to send data over the network.

whj
09-04-2009, 09:32 AM
I found a UDP Receive block in Simulink. It requires the other end of the communication, which is vizard, to specify the port that receives data. I'm not sure how to do it. I think my code only specifies the port that sends data. Am I missing something?


import viz
viz.go()

import socket

COMPUTER_IP_ADDRESS = socket.gethostbyname('192.59.88.91')

PORT = 25000 #The port to send data on

OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

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

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


Thanks!

farshizzo
09-04-2009, 04:29 PM
This post (http://forum.worldviz.com/showthread.php?t=1126) shows how to send and receive data on UDP sockets using different ports.

whj
09-05-2009, 08:49 PM
I'm confused, in the sample code, there is only one port 4999, which is "#The port to send/receive data on". Why does it show using different ports? Thanks.

whj
09-08-2009, 12:51 PM
So far I'm sure simulink on computer B can receive data from specified IP address and port over UDP. Then my question is, how do I specify the port that the remote computer B accepts data from in vizard on this computer A? Thanks.

whj
09-08-2009, 02:31 PM
I just got them communicated successfully. PORT = xxxx seems to be used to specify the remote port to receive from. Please forget my previous question.

My next problem is I would like to send two values from vizard to simulink every 0.05 second. My code would be like this:


import viz
viz.go()

import socket
import random

COMPUTER_IP_ADDRESS = '129.59.82.229'
PORT = 25000
OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

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

def mytimer(num):
value1 = random.randint(0, 70)
value2 = random.randint(1, 100)
SendData(value1, value2)


viz.callback(viz.TIMER_EVENT, mytimer)
viz.starttimer(0,0.05,viz.FOREVER)


I know SendData(value1, value2) wouldn't work. Could somebody tell me how to use SendDate at this context.

Thanks a lot.

farshizzo
09-09-2009, 12:16 PM
When sending data using the socket module, the data needs to be a string containing the raw byte data. Have a look at the Python struct module for packing Python integer/float values into raw byte data. The following example shows how to generate raw byte data from 2 integer values:import struct
data = struct.pack('ii',value1,value2)

whj
09-11-2009, 09:33 AM
Thanks a lot. That really helps.