Skip to content

Commit

Permalink
peppergamepad-1.0.4
Browse files Browse the repository at this point in the history
Add start() and stop() methods

Bug fixes

Warnings removal

Stop RemoteRobotController in onRobotFocusLost()

Update README
  • Loading branch information
Alexandre ROUX committed Sep 7, 2021
1 parent 74b2b1b commit 351634c
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 67 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ This video was filmed at SoftBank Robotics Europe, and shows the basic control s

## Getting Started


### Prerequisites

A robotified project for Pepper with QiSDK. Read the [documentation](https://developer.softbankrobotics.com/pepper-qisdk) if needed
Expand Down Expand Up @@ -41,7 +40,6 @@ Full implementation details are available to see in the sample project.

Make sure to replace 'Tag' by the number of the version of the library you want to use.


## Usage

*This README assumes some standard setup can be done by the user, such as initialising variables or implementing code in the correct functions. Refer to the Sample Project for full usage code.*
Expand All @@ -50,8 +48,7 @@ Initialise the QISDK in the onCreate. If you are unsure how to do this, refer to

QiSDK.register(this, this)

In the `onRobotFocusGained`, disable BasicAwareness, and instantiate a `RemoteRobotController` object by passing it the QiContext.

In the `onRobotFocusGained`, disable BasicAwareness, and instantiate a `RemoteRobotController` object by passing it the QiContext. Then start it.

```
override fun onRobotFocusGained(qiContext: QiContext) {
Expand All @@ -68,17 +65,13 @@ override fun onRobotFocusGained(qiContext: QiContext) {
}
remoteRobotController = RemoteRobotController(qiContext)
remoteRobotController.start()
}
```




Get the position of the controller and call updateTarget method. It is important to call this function in a thread, as it is using references to the QISDK.
```
remoteRobotController.updateTarget(leftJoystickX, leftJoystickY, rightJoystickX, rightJoystickY)
```

- Left joystick makes Pepper translate
- Right joystick makes Pepper rotate

Expand Down Expand Up @@ -126,6 +119,14 @@ private fun getCenteredAxis(event: MotionEvent, device: InputDevice, axis: Int):
}
```

You can stop `RemoteRobotController` object whenever you want by calling the `stop()` method. This can be helpfull if you want to run animations for instance:

```
remoteRobotController.stop()
myCustomAnimation.run()
remoteRobotController.start()
```

## License

This project is licensed under the BSD 3-Clause "New" or "Revised" License- see the [COPYING](COPYING.md) file for details
Expand Down
4 changes: 2 additions & 2 deletions pepper-gamepad-root/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
4 changes: 2 additions & 2 deletions pepper-gamepad-root/pepper-gamepad/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ apply plugin: 'kotlin-android'
android {
archivesBaseName = "pepper-gamepad"
version = "1.0.3"
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
targetSdkVersion 29
versionCode 4
versionName "1.0.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,49 @@ import kotlin.math.cos
import kotlin.math.roundToInt
import kotlin.math.sin

class RemoteRobotController(context: QiContext) {
class RemoteRobotController(private val qiContext: QiContext) {

private val TAG = "RemoteRobotController"

private val qiContext = context

var isRunning = false
private set
private var startAfterInitialization = false
private var isMoving = false

private var currentLeftJoystickX = 0
private var currentLeftJoystickY = 0
private var currentRightJoystickX = 0
private var currentRightJoystickY = 0

private var lookAtFuture: Future<Void>
private lateinit var lookAtFuture: Future<Void>
private lateinit var animateFuture: Future<Void>

private var defaultPosition: Transform
private var targetFrame: AttachedFrame
private var lookAt: LookAt
private lateinit var defaultPosition: Transform
private lateinit var targetFrame: AttachedFrame
private lateinit var lookAt: LookAt

init {
val robotFrame = qiContext.actuation.robotFrame()
defaultPosition = TransformBuilder.create().fromXTranslation(100.0)
targetFrame = robotFrame.makeAttachedFrame(defaultPosition)
lookAt = LookAtBuilder.with(qiContext).withFrame(targetFrame.frame()).build()
lookAt.addOnStartedListener {
Log.i(TAG, "LookAt started")
qiContext.actuation.async().gazeFrame().andThenConsume { robotFrame ->
defaultPosition = TransformBuilder.create().fromXTranslation(100.0)
targetFrame = robotFrame.makeAttachedFrame(defaultPosition)
lookAt = LookAtBuilder.with(qiContext).withFrame(targetFrame.frame()).build()
lookAt.addOnStartedListener {
Log.i(TAG, "LookAt started")
}
if (startAfterInitialization) start()
}
}

fun start() {
Log.d(TAG, "start")

if (!::lookAt.isInitialized) {
startAfterInitialization = true
return
}

if (isRunning) return

lookAtFuture = lookAt.async().run()
lookAtFuture.thenConsume {
when {
Expand All @@ -51,13 +67,33 @@ class RemoteRobotController(context: QiContext) {
lookAtFuture.isCancelled -> Log.e(TAG, "LookAt cancelled")
}
}

isRunning = true
}

fun updateTarget(newLeftJoystickX: Float, newLeftJoystickY: Float, newRightJoystickX: Float, newRightJoystickY: Float) {
Log.d(TAG, "updateTarget newLeftJoystickX=$newLeftJoystickX " +
"newLeftJoystickY=$newLeftJoystickY " +
"newRightJoystickX=$newRightJoystickX " +
"newRightJoystickY=$newRightJoystickY")
fun stop() {
Log.d(TAG, "stop")

if (!isRunning) return

lookAtFuture.requestCancellation()
animateFuture.requestCancellation()

isRunning = false
}

fun updateTarget(
newLeftJoystickX: Float,
newLeftJoystickY: Float,
newRightJoystickX: Float,
newRightJoystickY: Float
) {
Log.d(
TAG, "updateTarget newLeftJoystickX=$newLeftJoystickX " +
"newLeftJoystickY=$newLeftJoystickY " +
"newRightJoystickX=$newRightJoystickX " +
"newRightJoystickY=$newRightJoystickY"
)

// Round values
var roundedNewLeftJoystickX = 0
Expand Down Expand Up @@ -91,46 +127,59 @@ class RemoteRobotController(context: QiContext) {
}

private fun makeTranslation() {
Log.d(TAG, "makeTranslation currentLeftJoystickX=$currentLeftJoystickX currentLeftJoystickY=$currentLeftJoystickY")
Log.d(
TAG,
"makeTranslation currentLeftJoystickX=$currentLeftJoystickX currentLeftJoystickY=$currentLeftJoystickY"
)

if (!isRunning) return

if (::animateFuture.isInitialized && !animateFuture.isDone) {
animateFuture.requestCancellation()
} else if (!(currentLeftJoystickX == 0 && currentLeftJoystickY == 0) && !isMoving) {
isMoving = true
lookAt.policy = LookAtMovementPolicy.HEAD_ONLY

val targetX = -currentLeftJoystickY.toDouble()
val targetY = -currentLeftJoystickX.toDouble()

val animationString = "[\"Holonomic\", [\"Line\", [$targetX, $targetY]], 0.0, 40.0]"
val animation = AnimationBuilder.with(qiContext).withTexts(animationString).build()
val animate = AnimateBuilder.with(qiContext).withAnimation(animation).build()
animate.addOnStartedListener {
Log.i(TAG, "Animate started")

if (!(targetX == -currentLeftJoystickY.toDouble() && targetY == -currentLeftJoystickX.toDouble())) {
animateFuture.requestCancellation()
lookAt.async().setPolicy(LookAtMovementPolicy.HEAD_ONLY).andThenConsume {
val targetX = -currentLeftJoystickY.toDouble()
val targetY = -currentLeftJoystickX.toDouble()

val animationString = "[\"Holonomic\", [\"Line\", [$targetX, $targetY]], 0.0, 40.0]"
val animation = AnimationBuilder.with(qiContext).withTexts(animationString).build()
val animate = AnimateBuilder.with(qiContext).withAnimation(animation).build()
animate.addOnStartedListener {
Log.i(TAG, "Animate started")

if (!(targetX == -currentLeftJoystickY.toDouble() && targetY == -currentLeftJoystickX.toDouble())) {
animateFuture.requestCancellation()
}
}
}

animateFuture = animate.async().run()
animateFuture.thenConsume {
when {
animateFuture.isSuccess -> Log.i(TAG, "Animate finished with success")
animateFuture.hasError() -> Log.e(TAG, "Animate error: ${animateFuture.errorMessage}")
animateFuture.isCancelled -> Log.i(TAG, "Animate cancelled")
animateFuture = animate.async().run()
animateFuture.thenConsume {
when {
animateFuture.isSuccess -> Log.i(TAG, "Animate finished with success")
animateFuture.hasError() -> Log.e(
TAG,
"Animate error: ${animateFuture.errorMessage}"
)
animateFuture.isCancelled -> Log.i(TAG, "Animate cancelled")
}

lookAt.policy = LookAtMovementPolicy.HEAD_AND_BASE
isMoving = false

makeTranslation()
}

lookAt.policy = LookAtMovementPolicy.HEAD_AND_BASE
isMoving = false

makeTranslation()
}
}
}

private fun makeRotation() {
Log.d(TAG, "makeRotation currentRightJoystickX=$currentRightJoystickX currentRightJoystickY=$currentRightJoystickY")
Log.d(
TAG,
"makeRotation currentRightJoystickX=$currentRightJoystickX currentRightJoystickY=$currentRightJoystickY"
)

if (!isRunning) return

if (currentRightJoystickX == 0 && currentRightJoystickY == 0) {
targetFrame.update(defaultPosition)
Expand Down
4 changes: 2 additions & 2 deletions pepper-gamepad-root/sample-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.softbankrobotics.peppergamepadsample"
minSdkVersion 23
targetSdkVersion 28
targetSdkVersion 29
versionCode 2
versionName "1.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class MainActivity : Activity(), RobotLifecycleCallbacks, InputDeviceListener {
}
}
remoteRobotController = RemoteRobotController(qiContext)
Log.i(TAG, "after RemoteRobotController instantiation")
remoteRobotController.start()
}

override fun onGenericMotionEvent(event: MotionEvent): Boolean {
Expand All @@ -168,7 +168,12 @@ class MainActivity : Activity(), RobotLifecycleCallbacks, InputDeviceListener {

if (::remoteRobotController.isInitialized) {
thread {
remoteRobotController.updateTarget(leftJoystickX, leftJoystickY, rightJoystickX, rightJoystickY)
remoteRobotController.updateTarget(
leftJoystickX,
leftJoystickY,
rightJoystickX,
rightJoystickY
)
}
} else {
Log.d(TAG, "@@@@@@@@@ not initialized")
Expand Down Expand Up @@ -202,6 +207,7 @@ class MainActivity : Activity(), RobotLifecycleCallbacks, InputDeviceListener {
override fun onRobotFocusLost() {
Log.i(TAG, "onRobotFocusLost")
qiContext = null
remoteRobotController.stop()
}

override fun onRobotFocusRefused(reason: String?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Pepper Gamepad Sample</string>
<string name="no_controller_detected_message">Connectez une manette pour utiliser l\'application.</string>
<string name="no_controller_detected_title">Aucune manette détectée</string>
<string-array name="welcome">
<item>Génial, on part en balade! Prends ta manette, jette un oeil aux contrôles et on y va!</item>
<item>Super, j\'avais hâte de me dégourdir les roues! J\'espère que tu as ta manette, je t\'affiche un petit rappel des contrôles avant d\'y aller.</item>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<resources>
<string name="app_name">Pepper Gamepad Sample</string>
<string name="no_controller_detected_title">No controller detected</string>
<string name="no_controller_detected_message">Please connect a controller to use the app.</string>
<string-array name="welcome">
<item>Great, let\'s have fun together! Take the game pad, look at the controls and we\'re good to go!</item>
<item>Time to put these wheels to good use! I hope you have a game pad ready, take a look at the controls and let\'s go!</item>
Expand Down

0 comments on commit 351634c

Please sign in to comment.