Recommendations on getting started with VREP/Arduino

Typically: "How do I... ", "How can I... " questions
coppelia
Site Admin
Posts: 7445
Joined: 14 Dec 2012, 00:25

Re: Recommendations on getting started with VREP/Arduino

Post by coppelia » 22 Mar 2016, 09:26

You are opening the same port several times!!

First in the initialization phase, then in the actuation phase of your non-threaded child script.

A serial port is opened once only. After that you can read and write. Once you are done, you can close it with simSerialClose.

Do not mix-up threaded and non-threaded child scripts. Make sure you have understood how both work, otherwise you will get in trouble.

Cheers

dnahar
Posts: 30
Joined: 07 Jan 2016, 03:03

Re: Recommendations on getting started with VREP/Arduino

Post by dnahar » 22 Mar 2016, 14:00

This doesnt answer my question!! I see that i tried to open serial port in actuation phase also, which doesnt make sense for me too. The only reason is i couldnt access serial in the actuation phase which was opened during initialization, nor could i close it in the restoration phase if i access this serial. It throws me invalid porthandle error. (i could access simSerialRead without error but not SimSerialSend both in actuation phase)

Also please tell me,Could you tell me how do i assure myself the looping of the threaded child script actuation phase?? As i see when i print values from actuation phase in threaded script, i see only one value, which shows it executes just once.

Thanks,
Dixit

coppelia
Site Admin
Posts: 7445
Joined: 14 Dec 2012, 00:25

Re: Recommendations on getting started with VREP/Arduino

Post by coppelia » 22 Mar 2016, 16:33

You are mixing-up threaded and non-threaded child scripts! In your posts, you always talk about threaded scripts, but the code looks like non-threaded scripts.
A threaded script doesn't need (and will not execute the code) like in following example:

Code: Select all

-- If this is a threaded script, then:

if (sim_call_type==sim_childscriptcall_initialization) then 
    -- never executed
end 

if (sim_call_type==sim_childscriptcall_actuation) then 
    -- never executed
end 
Make sure you carefully read what the differences are between threaded and non-threaded script here.

Cheers

dnahar
Posts: 30
Joined: 07 Jan 2016, 03:03

Re: Recommendations on getting started with VREP/Arduino

Post by dnahar » 23 Mar 2016, 15:49

I went ahead with non-threaded script because i couldn't get solution in threaded one.

1. Could you tell me how looping is executed in threaded script: As i understand, code written in

Code: Select all


while (simGetSimulationState()~=sim_simulation_advancing_abouttostop) do
is executed during simulation many times. Here i put print statement and run the simulation, it prints just once. Can you explain this?

Thanks,
Dixit

coppelia
Site Admin
Posts: 7445
Joined: 14 Dec 2012, 00:25

Re: Recommendations on getting started with VREP/Arduino

Post by coppelia » 23 Mar 2016, 18:20

I am more and more confused. You write: I went ahead with non-threaded script...
but right after you write: Could you tell me how looping is executed in threaded script...

What are you using now? A threaded or non-threaded child script?

In a threaded child script you can do something like:

Code: Select all

-- Initialization here:
print('initialization')
a=0

-- The loop where we stay until simulation ends:
while simGetSimulationState()~=sim_simulation_advancing_abouttostop do
    a=a+1
    print(a)
    simSwitchThread() -- after this command, we resume the loop in next simulation step
end

-- Clean-up phase:
print('clean-up')
In a non-threaded child script, you do:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
    print('initialization')
    a=0
end

if (sim_call_type==sim_childscriptcall_actuation) then
    a=a+1
    print(a)
end

if (sim_call_type==sim_childscriptcall_sensing) then
    -- Instead of writing your code in the actuation phase, you can
    -- also write it in here, depending on what you do exactly
end

if (sim_call_type==sim_childscriptcall_cleanup) then
print('clean-up')
end
Cheers

dnahar
Posts: 30
Joined: 07 Jan 2016, 03:03

Re: Recommendations on getting started with VREP/Arduino

Post by dnahar » 23 Mar 2016, 19:02

Okay, i have done as you said in the threaded child script, but it just prints once. It doesn't print many times as expected as it is supposed to run in a loop. what can be the problem?

Code: Select all

------------------------------------------------------------------------------
-- The functions simMoveToPosition and simMoveToJointPositions are DEPRECATED.
-- Try using following much more powerful functions instead:
-- simRMLPosition or simRMLVelocity
------------------------------------------------------------------------------

simSetThreadSwitchTiming(2) -- Default timing for automatic thread switching

h=simGetObjectAssociatedWithScript(sim_handle_self)
v=0.1
a=1
p=simGetObjectPosition(h,-1)
o=simGetObjectOrientation(h,-1)

-- for serial port

dialog,uinum = simDisplayDialog("Start Serial Communication","Serial Port = ", sim_dlgstyle_input, true, "COM4",nil,nil)
port = simGetDialogInput(dialog) 

portNumber="\\\\.\\"
portNumber = portNumber..port

result=simEndDialog(dialog)

baudrate=19200

serial=simSerialOpen(portNumber,baudrate)
simWait(1,true)

-- for UI

if (simGetScriptExecutionCount()==0) then
   uiHandle=simGetUIHandle('UI')
end

while (simGetSimulationState()~=sim_simulation_advancing_abouttostop) do

button,auxData=simGetUIEventButton(uiHandle)

-- update sensor weights
--for i = 1,9,1 do
	str0 = simSerialRead(serial,10,true,'\n',10)
	print(str0)
	simSetUIButtonLabel(uiHandle,25,str0,"")
--end

	if str0 ~= nil then
		print(str0)
		simSetUIButtonLabel(uiHandle,27,str0,"")
	end
		--simSetUIButtonLabel(uiHandle,27,"100","")

if (button==3) then
	signal="9"
	simSerialSend(serial,signal) 
elseif (button==4) then
	signal="A"
	simSerialSend(serial,signal)
elseif (button==5) then
	signal="1"
	simSerialSend(serial,signal) 
elseif (button==6) then
	signal="B"
	simSerialSend(serial,signal) 
elseif (button==7) then
	signal="2"
	simSerialSend(serial,signal) 
elseif (button==8) then
	signal="C"
	simSerialSend(serial,signal) 
elseif (button==9) then
	signal="3"
	simSerialSend(serial,signal) 
elseif (button==10) then
	signal="D"
	simSerialSend(serial,signal) 
elseif (button==11) then
	signal="4"
	simSerialSend(serial,signal) 
elseif (button==12) then
	signal="E"
	simSerialSend(serial,signal) 
elseif (button==13) then
	signal="5"
	simSerialSend(serial,signal)
elseif (button==14) then
	signal="F"
	simSerialSend(serial,signal)
elseif (button==15) then
	signal="6"
	simSerialSend(serial,signal)
elseif (button==16) then
	signal="G"
	simSerialSend(serial,signal)
elseif (button==17) then
	signal="7"
	simSerialSend(serial,signal)
elseif (button==18) then
	signal="H"
	simSerialSend(serial,signal)
elseif (button==19) then
	signal="8"
	simSerialSend(serial,signal)
elseif (button==20) then
	signal="I"
	simSerialSend(serial,signal)  
elseif (button==21) then
	signal="J"
	simSerialSend(serial,signal)
elseif (button==22) then
	signal="L"
	simSerialSend(serial,signal)
elseif (button==23) then
	signal="K"
	simSerialSend(serial,signal)
elseif (button==24) then
	signal="M"
	simSerialSend(serial,signal)
-- wind to threshold
elseif (button==34) then
	signal="N"
	simSerialSend(serial,signal)
-- send motor selection
	str0 = simGetUIButtonLabel(uiHandle,41)
	str0 = str0..c
	simSerialSend(serial,str0)
-- send threshold tension
	str0 = simGetUIButtonLabel(uiHandle,43)
	str0 = str0..c
	simSerialSend(serial,str0)
-- sensor weight update
for i = 1,9,1 do
	str0 = simSerialRead(serial,100,true,'\n',100)
	simSetUIButtonLabel(uiHandle,24+i,str0,str0)
end

elseif (button==35) then
	signal="O"
	simSerialSend(serial,signal)
-- send motor selection
	str0 = simGetUIButtonLabel(uiHandle,41)
	simSerialSend(serial,str0)
-- sensor weight update
for i = 1,9,1 do
	str0 = simSerialRead(serial,100,true,'\n',100)
	simSetUIButtonLabel(uiHandle,24+i,str0)
end

while str0 ~= ',' do
	for i = 1,9,1 do
		str0 = simSerialRead(serial,100,true,'\n',100)
		simSetUIButtonLabel(uiHandle,24+i,str0)
	end
end
--circumnutation
elseif (button==36) then
	signal="P"
	simSerialSend(serial,signal)
-- send motor selection
	str0 = simGetUIButtonLabel(uiHandle,41)
	simSerialSend(serial,str0)

-- sensor weight update
for i = 1,9,1 do
	str0 = simSerialRead(serial,100,true,'\n',100)
	simSetUIButtonLabel(uiHandle,25+i,str0,str0)
end

while str0 ~= ',' do
	for i = 1,9,1 do
		str0 = simSerialRead(serial,100,true,'\n',100)
		simSetUIButtonLabel(uiHandle,24+i,str0)
	end
end

elseif (button==36) then
	signal="Q"
	simSerialSend(serial,signal)

-- sensor weight update
for i = 1,9,1 do
	str0 = simSerialRead(serial,100,true,'\n',100)
	simSetUIButtonLabel(uiHandle,25+i,str0,str0)
end
-- sensor weight update after tare
for i = 1,9,1 do
	str0 = simSerialRead(serial,100,true,'\n',100)
	simSetUIButtonLabel(uiHandle,25+i,str0,str0)
end

elseif (button==47) then
	signal="R"
	simSerialSend(serial,signal)
	
	str0 = simSerialRead(serial,100,true,'\n',1000)
	print(str0)
	simSetUIButtonLabel(uiHandle,25,str0,"")
	print('10')
elseif (button==40) then
	signal="S"
	simSerialSend(serial,signal)
-- get servo time
	c = ","
	str0 = simGetUIButtonLabel(uiHandle,39)
	str0 = str0..c
	simSerialSend(serial,str0)
-- get motor selection
	str0 = simGetUIButtonLabel(uiHandle,41)
	str0 = str0..c
	simSerialSend(serial,str0)

--thats it for UI
	-- not wasting time here, we force the switching of the thread here
	simSwitchThread()
end
end

simSerialClose(serial)


for i=1,3,1 do
	p[1]=p[1]-0.15
	simMoveToPosition(h,-1,p,o,v,a)
	--simRMLPosition(h,-1,p,o,v,a)
	o[3]=o[3]+1.3
	simMoveToPosition(h,-1,p,o,v,a)
	o[2]=o[2]+1.25
	simMoveToPosition(h,-1,p,o,v,a)
	p[1]=p[1]+0.15
	o[3]=o[3]-1.3
	o[2]=o[2]-1.25
	simMoveToPosition(h,-1,p,o,v,a)
end
line number 43 is where i print.

Thanks,
Dixit

coppelia
Site Admin
Posts: 7445
Joined: 14 Dec 2012, 00:25

Re: Recommendations on getting started with VREP/Arduino

Post by coppelia » 23 Mar 2016, 19:07

Did you try the example I provided? Does it print once or many times?

Cheers

dnahar
Posts: 30
Joined: 07 Jan 2016, 03:03

Re: Recommendations on getting started with VREP/Arduino

Post by dnahar » 30 Mar 2016, 20:18

Okay, so one of my colleague suggested to try my code in a different device(computer) and to my surprise, serial communication is working perfectly.

Now on my device, i used to be able to just send data to arduino, but couldn't receive anything from it. I still face this issue. Really, what is going on here? (I checked device managers of both PCs, they are the same for the serial port)

Thanks,
Dixit

coppelia
Site Admin
Posts: 7445
Joined: 14 Dec 2012, 00:25

Re: Recommendations on getting started with VREP/Arduino

Post by coppelia » 31 Mar 2016, 10:19

Ok, it seems we are not moving ahead here.

So what you can do, is use a Lua extension library for the serial port communication. Here a related link.

Cheers

dnahar
Posts: 30
Joined: 07 Jan 2016, 03:03

Re: Recommendations on getting started with VREP/Arduino

Post by dnahar » 14 Apr 2016, 20:11

Could this be a Windows problem. I am using Windows 10.

Also, could you help me move this attached simulation. I am not able to move as shown in inverse kinematics page where they show how we move the target dummy during run and the manipulator follows. Tell me where i am wrong.

https://www.dropbox.com/s/fegflw86fbvd ... .ttt?dl=0

Thanks,
Dixit Nahar

Post Reply