Skip to content

Commit 0e4efff

Browse files
committed
add remote function calls, small refactor, update README
1 parent 4df908c commit 0e4efff

29 files changed

+461
-176
lines changed

README.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ Simple Scala binding for
55
## Getting started
66
1. Add the library in your sbt project by simply adding the following dependency to your build file:
77
```scala
8-
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
9-
10-
libraryDependencies += "com.github.troxid" %% "vrepapiscala" % "0.1-SNAPSHOT"
8+
libraryDependencies += "com.github.troxid" %% "vrepapiscala" % "0.2.1"
119
```
1210
2. Copy platform-specific native library from
1311
`V-REP/programming/remoteApiBindings/java/lib/`
@@ -23,12 +21,12 @@ Basically implemented those components that are required to control the robot:
2321
* Proximity sensor
2422
* Vision sensor
2523
* Force sensor
26-
* Navigation sensor (used for that dummy object)
24+
* Position sensor (used for that dummy or shape object)
25+
* Remote function calls
2726

2827
## Example
2928
```scala
3029
import vrepapiscala._
31-
import vrepapiscala.sensors.ProximitySensor
3230

3331
object PioneerRemoteControlExample extends App {
3432
val api = VRepAPI.connect("127.0.0.1", 19997).get
@@ -37,27 +35,28 @@ object PioneerRemoteControlExample extends App {
3735
api.simulation.start()
3836

3937
for(_ <- 0 to 500){
40-
Thread.sleep(10)
41-
robot.leftAndRightSensor match {
42-
case (ProximitySensor.Values(true, dp, _, _), _) if dp.length < 0.5=>
43-
robot.rotateRight()
44-
case (_, ProximitySensor.Values(true, dp, _, _)) if dp.length < 0.5=>
45-
robot.rotateLeft()
46-
case _ =>
47-
robot.moveForward()
38+
val resLS = robot.leftSensor.read
39+
val resRS = robot.rightSensor.read
40+
if(resLS.detectionState && resLS.detectedPoint.length < 0.5){
41+
robot.rotateRight()
42+
}else if(resRS.detectionState && resRS.detectedPoint.length < 0.5){
43+
robot.rotateLeft()
44+
} else {
45+
robot.moveForward()
4846
}
47+
Thread.sleep(10)
4948
}
5049

5150
api.simulation.stop()
5251
}
5352

5453
class PioneerP3dx(api: VRepAPI) {
5554
private val speed = 2f
56-
private val leftMotor = api.joint.withVelocityControl("Pioneer_p3dx_leftMotor")
57-
private val rightMotor = api.joint.withVelocityControl("Pioneer_p3dx_rightMotor")
55+
private val leftMotor = api.joint.withVelocityControl("Pioneer_p3dx_leftMotor").get
56+
private val rightMotor = api.joint.withVelocityControl("Pioneer_p3dx_rightMotor").get
5857
private val frontSensors =
5958
for(i <- 1 to 8)
60-
yield api.sensor.proximitySensor("Pioneer_p3dx_ultrasonicSensor" + i)
59+
yield api.sensor.proximity("Pioneer_p3dx_ultrasonicSensor" + i).get
6160

6261
def moveForward(): Unit = {
6362
leftMotor.setTargetVelocity(speed)
@@ -84,7 +83,9 @@ class PioneerP3dx(api: VRepAPI) {
8483
rightMotor.setTargetVelocity(0)
8584
}
8685

87-
def leftAndRightSensor = (frontSensors(1).read, frontSensors(6).read)
86+
def leftSensor = frontSensors(1)
87+
88+
def rightSensor = frontSensors(6)
8889
}
8990
```
9091

build.sbt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
12
name := "vrepapiscala"
23

3-
version := "0.1-SNAPSHOT"
4+
version := "0.2.1"
45

5-
scalaVersion := "2.11.7"
6+
scalaVersion := "2.11.8"
67

78
organization := "com.github.troxid"
89

examples/PioneerRemoteControlExample.scala examples/src/main/scala/vrepapiscala/example/PioneerRemoteControlExample.scala

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* Created by troxid on 09.06.2015.
33
*/
4+
package vrepapiscala.example
45

5-
import vrepapiscala._
6-
import vrepapiscala.sensors.ProximitySensor
6+
import vrepapiscala.VRepAPI
77

88
object PioneerRemoteControlExample extends App {
99
val api = VRepAPI.connect("127.0.0.1", 19997).get
@@ -12,27 +12,28 @@ object PioneerRemoteControlExample extends App {
1212
api.simulation.start()
1313

1414
for(_ <- 0 to 500){
15-
Thread.sleep(10)
16-
robot.leftAndRightSensor match {
17-
case (ProximitySensor.Values(true, dp, _, _), _) if dp.length < 0.5=>
18-
robot.rotateRight()
19-
case (_, ProximitySensor.Values(true, dp, _, _)) if dp.length < 0.5=>
20-
robot.rotateLeft()
21-
case _ =>
22-
robot.moveForward()
15+
val resLS = robot.leftSensor.read
16+
val resRS = robot.rightSensor.read
17+
if(resLS.detectionState && resLS.detectedPoint.length < 0.5){
18+
robot.rotateRight()
19+
}else if(resRS.detectionState && resRS.detectedPoint.length < 0.5){
20+
robot.rotateLeft()
21+
} else {
22+
robot.moveForward()
2323
}
24+
Thread.sleep(10)
2425
}
2526

2627
api.simulation.stop()
2728
}
2829

2930
class PioneerP3dx(api: VRepAPI) {
3031
private val speed = 2f
31-
private val leftMotor = api.joint.withVelocityControl("Pioneer_p3dx_leftMotor")
32-
private val rightMotor = api.joint.withVelocityControl("Pioneer_p3dx_rightMotor")
32+
private val leftMotor = api.joint.withVelocityControl("Pioneer_p3dx_leftMotor").get
33+
private val rightMotor = api.joint.withVelocityControl("Pioneer_p3dx_rightMotor").get
3334
private val frontSensors =
3435
for(i <- 1 to 8)
35-
yield api.sensor.proximitySensor("Pioneer_p3dx_ultrasonicSensor" + i)
36+
yield api.sensor.proximity("Pioneer_p3dx_ultrasonicSensor" + i).get
3637

3738
def moveForward(): Unit = {
3839
leftMotor.setTargetVelocity(speed)
@@ -59,7 +60,8 @@ class PioneerP3dx(api: VRepAPI) {
5960
rightMotor.setTargetVelocity(0)
6061
}
6162

62-
def leftAndRightSensor = (frontSensors(1).read, frontSensors(6).read)
63+
def leftSensor = frontSensors(1)
6364

65+
def rightSensor = frontSensors(6)
6466
}
6567

src/main/java/coppelia/remoteApi.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of the REMOTE API
22
//
3-
// Copyright 2006-2015 Coppelia Robotics GmbH. All rights reserved.
3+
// Copyright 2006-2016 Coppelia Robotics GmbH. All rights reserved.
44
// marc@coppeliarobotics.com
55
// www.coppeliarobotics.com
66
//
@@ -24,7 +24,7 @@
2424
// along with the REMOTE API. If not, see <http://www.gnu.org/licenses/>.
2525
// -------------------------------------------------------------------
2626
//
27-
// This file was automatically created for V-REP release V3.2.3 rev4 on December 21st 2015
27+
// This file was automatically created for V-REP release V3.3.0 on February 19th 2016
2828

2929
package coppelia;
3030

@@ -142,6 +142,7 @@ public class remoteApi
142142
public native int simxQuery(int clientID,final String signalName, final CharWA signalValue,final String retSignalName, CharWA retSignalValue, int timeOutInMs);
143143
public native int simxGetObjectGroupData(int clientID,int objectType,int dataType,IntWA handles,IntWA intData,FloatWA floatData,StringWA stringData,int operationMode);
144144
public native int simxGetObjectVelocity(int clientID,int objectHandle, FloatWA linearVelocity, FloatWA angularVelocity, int operationMode);
145+
public native int simxCallScriptFunction(int clientID,final String scriptDescription,int options,final String functionName,final IntWA inInts,final FloatWA inFloats,final StringWA inStrings,final CharWA inBuffer,IntWA outInts,FloatWA outFloats,StringWA outStrings,CharWA outBuffer,int operationMode);
145146

146147

147148
public static final int SIMX_HEADER_SIZE = 18;
@@ -408,7 +409,6 @@ public class remoteApi
408409
public static final int sim_simulation_advancing = 16; /* Simulation is advancing */
409410
public static final int sim_simulation_advancing_firstafterstop =16 |0; /* First simulation pass (1x) */
410411
public static final int sim_simulation_advancing_running = 16|1; /* Normal simulation pass (>=1x) */
411-
/* reserved =sim_simulation_advancing|0x02, */
412412
public static final int sim_simulation_advancing_lastbeforepause = 16|3; /* Last simulation pass before pause (1x) */
413413
public static final int sim_simulation_advancing_firstafterpause = 16|4; /* First simulation pass after pause (1x) */
414414
public static final int sim_simulation_advancing_abouttostop = 16|5; /* "Trying to stop" simulation pass (>=1x) */
@@ -425,9 +425,10 @@ public class remoteApi
425425
/* Script types (serialized!) */
426426
public static final int sim_scripttype_mainscript = 0;
427427
public static final int sim_scripttype_childscript = 1;
428-
public static final int sim_scripttype_pluginscript = 2;
429-
public static final int sim_scripttype_threaded = 240; /* Combine with one of above's type values */
430-
428+
public static final int sim_scripttype_jointctrlcallback = 4;
429+
public static final int sim_scripttype_contactcallback = 5;
430+
public static final int sim_scripttype_customizationscript = 6;
431+
public static final int sim_scripttype_generalcallback = 7;
431432

432433
/* API call error messages */
433434
public static final int sim_api_errormessage_ignore = 0; /* does not memorize nor output errors */
@@ -723,7 +724,7 @@ public class remoteApi
723724
/* Command return codes */
724725
public static final int simx_return_ok = 0;
725726
public static final int simx_return_novalue_flag = 1; /* input buffer doesn't contain the specified command */
726-
public static final int simx_return_timeout_flag = 2; /* command reply not received in time for simx_opmode_oneshot_wait operation mode */
727+
public static final int simx_return_timeout_flag = 2; /* command reply not received in time for simx_opmode_blocking operation mode */
727728
public static final int simx_return_illegal_opmode_flag = 4; /* command doesn't support the specified operation mode */
728729
public static final int simx_return_remote_error_flag = 8; /* command caused an error on the server side */
729730
public static final int simx_return_split_progress_flag = 16; /* previous similar command not yet fully processed (applies to simx_opmode_oneshot_split operation modes) */
@@ -733,7 +734,7 @@ public class remoteApi
733734
/* Following for backward compatibility (same as above) */
734735
public static final int simx_error_noerror = 0;
735736
public static final int simx_error_novalue_flag = 1; /* input buffer doesn't contain the specified command */
736-
public static final int simx_error_timeout_flag = 2; /* command reply not received in time for simx_opmode_oneshot_wait operation mode */
737+
public static final int simx_error_timeout_flag = 2; /* command reply not received in time for simx_opmode_blocking operation mode */
737738
public static final int simx_error_illegal_opmode_flag = 4; /* command doesn't support the specified operation mode */
738739
public static final int simx_error_remote_error_flag = 8; /* command caused an error on the server side */
739740
public static final int simx_error_split_progress_flag = 16; /* previous similar command not yet fully processed (applies to simx_opmode_oneshot_split operation modes) */
@@ -743,6 +744,7 @@ public class remoteApi
743744

744745
/* Regular operation modes */
745746
public static final int simx_opmode_oneshot = 0; /* sends command as one chunk. Reply will also come as one chunk. Doesn't wait for the reply. */
747+
public static final int simx_opmode_blocking = 65536; /* sends command as one chunk. Reply will also come as one chunk. Waits for the reply (_REPLY_WAIT_TIMEOUT_IN_MS is the timeout). */
746748
public static final int simx_opmode_oneshot_wait = 65536; /* sends command as one chunk. Reply will also come as one chunk. Waits for the reply (_REPLY_WAIT_TIMEOUT_IN_MS is the timeout). */
747749
public static final int simx_opmode_streaming = 131072;
748750
public static final int simx_opmode_continuous = 131072;

src/main/scala/vrepapiscala/CommandReturnCode.scala

+37-17
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,45 @@ import coppelia.remoteApi
55
/**
66
* Created by troxid on 22.11.15.
77
*/
8-
sealed trait CommandReturnCode
8+
sealed abstract class CommandReturnCode(val msg: String)
99
object CommandReturnCode {
10-
case class Ok() extends CommandReturnCode
11-
case class NoValue() extends CommandReturnCode
12-
case class TimeOut() extends CommandReturnCode
13-
case class IllegalOpMode() extends CommandReturnCode
14-
case class RemoteError() extends CommandReturnCode
15-
case class SplitProgress() extends CommandReturnCode
16-
case class LocalError() extends CommandReturnCode
17-
case class InitializeError() extends CommandReturnCode
10+
case object Ok
11+
extends CommandReturnCode("Ok")
12+
13+
case object NoValue
14+
extends CommandReturnCode("Input buffer doesn't contain the specified command")
15+
16+
case object TimeOut
17+
extends CommandReturnCode("Command reply not received in time for opmode_oneshot_wait operation mode")
18+
19+
case object IllegalOpMode
20+
extends CommandReturnCode("Command doesn't support the specified operation mode")
21+
22+
case object RemoteError
23+
extends CommandReturnCode("Command caused an error on the server side")
24+
25+
case object SplitProgress
26+
extends CommandReturnCode("Previous similar command not yet fully processed " +
27+
"(applies to opmode_oneshot_split operation modes)")
28+
29+
case object LocalError
30+
extends CommandReturnCode("Command caused an error on the client side")
31+
32+
case object InitializeError
33+
extends CommandReturnCode("simxStart was not yet called")
34+
35+
case class UndefCode(code: Int)
36+
extends CommandReturnCode(s"Undefined return code: $code")
1837

1938
def fromRaw(code: Int): CommandReturnCode = code match {
20-
case remoteApi.simx_return_ok => Ok()
21-
case remoteApi.simx_return_novalue_flag => NoValue()
22-
case remoteApi.simx_return_timeout_flag => TimeOut()
23-
case remoteApi.simx_return_illegal_opmode_flag => IllegalOpMode()
24-
case remoteApi.simx_return_remote_error_flag => RemoteError()
25-
case remoteApi.simx_return_split_progress_flag => SplitProgress()
26-
case remoteApi.simx_return_local_error_flag => LocalError()
27-
case remoteApi.simx_return_initialize_error_flag => InitializeError()
39+
case remoteApi.simx_return_ok => Ok
40+
case remoteApi.simx_return_novalue_flag => NoValue
41+
case remoteApi.simx_return_timeout_flag => TimeOut
42+
case remoteApi.simx_return_illegal_opmode_flag => IllegalOpMode
43+
case remoteApi.simx_return_remote_error_flag => RemoteError
44+
case remoteApi.simx_return_split_progress_flag => SplitProgress
45+
case remoteApi.simx_return_local_error_flag => LocalError
46+
case remoteApi.simx_return_initialize_error_flag => InitializeError
47+
case undefCode => UndefCode(code)
2848
}
2949
}

src/main/scala/vrepapiscala/OpMode.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package vrepapiscala
22

33
import coppelia.remoteApi
4-
import coppelia.remoteApi._
54

65
/**
76
* Created by troxid on 22.11.15.
@@ -16,7 +15,7 @@ object OpMode {
1615
* @note Non-blocking mode.
1716
* @note Regular operation modes.
1817
*/
19-
case object OneShot extends OpMode(simx_opmode_oneshot)
18+
case object OneShot extends OpMode(simx_opmode_oneshot)
2019

2120
/** The command is sent, and the function will wait for the actual reply
2221
* and return it (if the function doesn't time out). The received command reply will be removed
@@ -33,7 +32,7 @@ object OpMode {
3332
* @note Non-blocking mode.
3433
* @note Regular operation modes.
3534
*/
36-
case class Streaming(msDelay: Int = 0) extends OpMode(simx_opmode_streaming + msDelay){
35+
case class Streaming(msDelay: Int = 0) extends OpMode(simx_opmode_streaming + msDelay){
3736
require(msDelay >= 0 && msDelay <= 65535, "Alpha must be between 0-65535")
3837
}
3938

@@ -50,7 +49,7 @@ object OpMode {
5049
* @note Operation modes for heavy data.
5150
* @note Not recommended
5251
*/
53-
case class OneShotSplit(chunkSize: Int = 100) extends OpMode(simx_opmode_oneshot_split + chunkSize){
52+
case class OneShotSplit(chunkSize: Int = 100) extends OpMode(simx_opmode_oneshot_split + chunkSize){
5453
require(chunkSize >= 100 && chunkSize <= 65535, "Beta must be between 100-65535")
5554
}
5655

@@ -65,7 +64,7 @@ object OpMode {
6564
* @note Operation modes for heavy data.
6665
* @note Not recommended
6766
*/
68-
case class StreamingSplit(chunkSize: Int = 100) extends OpMode(simx_opmode_streaming_split + chunkSize){
67+
case class StreamingSplit(chunkSize: Int = 100) extends OpMode(simx_opmode_streaming_split + chunkSize){
6968
require(chunkSize >= 100 && chunkSize <= 65535, "Beta must be between 100-65535")
7069
}
7170

@@ -86,7 +85,7 @@ object OpMode {
8685
* @note Non-blocking mode.
8786
* @note Special operation modes
8887
*/
89-
case object Buffer extends OpMode(simx_opmode_buffer)
88+
case object Buffer extends OpMode(simx_opmode_buffer)
9089

9190
/**
9291
* A previous reply to the same command is cleared from the input buffer (if available).
@@ -95,5 +94,5 @@ object OpMode {
9594
* @note Non-blocking mode.
9695
* @note Special operation modes
9796
*/
98-
case object Remove extends OpMode(simx_opmode_remove)
97+
case object Remove extends OpMode(simx_opmode_remove)
9998
}

0 commit comments

Comments
 (0)