UI buttons

Typically: "How do I... ", "How can I... " questions
Post Reply
molerat
Posts: 10
Joined: 24 Mar 2014, 21:31

UI buttons

Post by molerat » 23 Apr 2014, 19:48

Hi there,
I have got problems with implementing GUI for my robot. I want to use 4 event buttons (forward, backward, left, right).
ctrl=simGetUIHandle("GUI") -- handle for the GUI
...
f=simGetUIEventButton(ctrl,4)
b=simGetUIEventButton(ctrl,5)
l=simGetUIEventButton(ctrl,3)
r=simGetUIEventButton(ctrl,6)
-- 3 to 6 are the button handles
I want my robot to go forward, If button 4 is pressed, etc.

*Should I use up/down event buttons, or checkable buttons?
*Could you help me with the code? How can I get the current status of buttons?
thank you

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

Re: UI buttons

Post by coppelia » 23 Apr 2014, 21:02

Hello,
  • if you click a button, and you want it to emit a single event, just keep the default button.
  • if you click a button, and you want it to stay down after that, you need the Checkable property.
  • if you click a button, and you want to differentiate press and release events, you need the up/down event property
For instance:

Code: Select all

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

button,auxData=simGetUIEventButton(uiHandle)

if (button==buttonHandle) then
	-- simple button was clicked (event at mouse up)
	-- or
	-- checkable button was clicked (event at mouse up)
	-- or
	-- simple button with up/down event was clicked (event at mouse up/down)
	--
	-- in auxData you have additional information
end
Check the simGetUIEventButton function for details.

Check also the simGetUIButtonProperty function.

Cheers

molerat
Posts: 10
Joined: 24 Mar 2014, 21:31

Re: UI buttons

Post by molerat » 24 Apr 2014, 07:08

Hi,
thank you for fast reply. I have checked the functions, and corrected my button handles. It's still not working. Could you explain me this line of code?

if (buttonHandle==buttonHandle) then

I thought buttonHandle must be compared to a value (0 or 1) to get the button's current status.
Thanks

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

Re: UI buttons

Post by coppelia » 24 Apr 2014, 08:59

Oops, that was a typo. It is now corrected, sorry about that.

Cheers

molerat
Posts: 10
Joined: 24 Mar 2014, 21:31

Re: UI buttons

Post by molerat » 24 Apr 2014, 11:39

It's okay. I' m still doing something wrong.
I want to control my robot with 4 up/down event buttons(if "up" is pressed, do robot should go forward, if "left", it should change his direction). For setting joint velocities I use simSetJointTargetVelocity(). What am I doing wrong? My code is:

if (simGetScriptExecutionCount()==0) then
robot=simGetObjectAssociatedWithScript(sim_handle_self)
ctrl=simGetUIHandle("GUI")
-- handles of joints, and definition of speed
fhandle=4 --forward
bhandle=5 --back
lhandle=3 --left
rhandle=6 --right
end
-- handles of event buttons
bf,aux=simGetUIEventButton(ctrl)
bb,aux2=simGetUIEventButton(ctrl)
bl,aux3=simGetUIEventButton(ctrl)
br,aux4=simGetUIEventButton(ctrl)

--detection of button statuses
if (bf == fhandle) then
--setting of target velocities, like this: simSetJointTargetVelocity(left,speed)
end

if (bb == bhandle) then
-- setting target velocities to: -speed
end


* I think I'm doing something wrong with ''if''-s, or with the detection of button events. Could you help me?
* Later, I would like to add limited execution time, something like:
time=simGetSimulationTime()+2
while (simGetSimulationTime()< time ) do
-- setting of joint target velocities
end
Thanks for all

Eric
Posts: 186
Joined: 11 Feb 2013, 16:39

Re: UI buttons

Post by Eric » 24 Apr 2014, 16:26

Hi

You did not quite get how the simGetUIEventButton(UIhandle) function is working. It catches events that occurred on any button of the UI.

If you want to detect what is the last button that has been clicked, use the following code (don t put the buttons in up/down events or you might enter several times in the corresponding "setting of target velocities" if condition, which actually is not a problem for your application).

Code: Select all

if (simGetScriptExecutionCount()==0) then
  robot=simGetObjectAssociatedWithScript(sim_handle_self)
  ctrl=simGetUIHandle("GUI")
  -- handles of joints, and definition of speed
  fhandle=4 --forward
  bhandle=5 --back
  lhandle=3 --left
  rhandle=6 --right

 starttime=simGetSimulationTime()+2

end
-- get the handles of event buttons
event=simGetUIEventButton(ctrl)

--detection of button statuses
if (event == lhandle) then
  --setting of target velocities, like this: simSetJointTargetVelocity(left,speed)
  simAddStatusbarMessage('left')
end

if (event == bhandle) then
  -- setting target velocities to: -speed
  simAddStatusbarMessage('back')
end

if  simGetSimulationTime()>=starttime then
  simStopSimulation()
end
Be aware that using simGetSimulationTime() will return the simulation time, not the real time (unless you click on the RT button before playing the simulation), meaning that if your simulation is light and /or your computer is fast, simulation seconds are going to go very fast. If you want to run your simulation for some real seconds use simGetSystemTime() instead of simGetSimulationTime() or press the RT button to make V-REP try to run the simulation in real time (if the simulation is too heavy or your computer too slow, you ll get seconds taking longer than real seconds).

I hope it s clear

Cheers

Eric

molerat
Posts: 10
Joined: 24 Mar 2014, 21:31

Re: UI buttons

Post by molerat » 24 Apr 2014, 19:17

Oh, now I got it, everything is clear. I appreciate your help :)

Post Reply