Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading Zenoh dependency to 1.1.0 on the Robot Teleop app #25

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zenoh-android/RobotTeleop/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
}

dependencies {
implementation("io.zenoh:zenoh-kotlin-android:0.11.0")
implementation("org.eclipse.zenoh:zenoh-kotlin-android:1.1.0")
implementation("commons-net:commons-net:3.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("androidx.core:core-ktx:1.9.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import androidx.lifecycle.ViewModelProvider
import com.example.zenohapp.databinding.ActivityMainBinding
import io.zenoh.Config
import io.zenoh.Session
import io.zenoh.Zenoh
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import kotlin.io.path.Path

class MainActivity : AppCompatActivity() {
Expand All @@ -32,16 +32,17 @@ class MainActivity : AppCompatActivity() {

viewModel = ViewModelProvider(this).get(ZenohViewModel::class.java)

System.setProperty("zenoh.logger", "debug")
Zenoh.initLogFromEnvOr("debug")

val config = assets.open("config-inline-prod.json")
val configFile = assets.open("config-inline-prod.json")
val tempConfig = File.createTempFile("config", ".json5")
tempConfig.deleteOnExit()
FileOutputStream(tempConfig).use { output ->
config.copyTo(output)
configFile.copyTo(output)
}

Session.open(Config.Companion.from(Path(tempConfig.absolutePath))).onSuccess {
val config = Config.fromFile(Path(tempConfig.absolutePath)).getOrThrow()
Zenoh.open(config).onSuccess {
viewModel.zenohSession = it
}.onFailure {
Log.e("Zenoh Session", "Zenoh session could not be opened: ${it.message}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import com.example.zenohapp.ros.Image
import com.example.zenohapp.ros.Time
import com.example.zenohapp.ros.Twist
import com.example.zenohapp.ros.Vector3
import io.zenoh.bytes.Encoding
import io.zenoh.bytes.ZBytes
import io.zenoh.keyexpr.intoKeyExpr
import io.zenoh.prelude.Encoding
import io.zenoh.prelude.KnownEncoding
import io.zenoh.publication.Publisher
import io.zenoh.pubsub.Publisher
import io.zenoh.pubsub.Subscriber
import io.zenoh.query.intoSelector
import io.zenoh.sample.Sample
import io.zenoh.subscriber.Subscriber
import io.zenoh.value.Value
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
Expand All @@ -49,10 +49,6 @@ import kotlinx.coroutines.launch
import java.nio.ByteBuffer
import kotlin.random.Random





class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
private var _binding: FragmentTeleopBinding? = null
private val binding get() = _binding!!
Expand Down Expand Up @@ -219,9 +215,9 @@ class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
mKeyExpr = mNSEditText.text.toString()+cmdVelSuffix
viewModel.zenohSession?.apply {
mKeyExpr.intoKeyExpr().onSuccess { ke ->
this.declarePublisher(ke).res().onSuccess { pub ->
this.declarePublisher(ke).onSuccess { pub ->
mPublisher = pub
Log.v(TAG, "Declared publiser on: ${mNSEditText.text.toString()}")
Log.v(TAG, "Declared publiser on: ${mNSEditText.text}")
}
.onFailure { handleError(TAG, "Failed to launch publisher", it) }
}
Expand All @@ -237,9 +233,10 @@ class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
val arr = ByteArray(mStream.buffer.position())
mStream.buffer.rewind()
mStream.buffer.get(arr)
val payload = Value(arr, Encoding(KnownEncoding.APP_OCTET_STREAM))
val payload = ZBytes.from(arr)

mPublisher.put(payload).res().onFailure { Log.e(TAG, "Error when publishing", it) }
mPublisher.put(payload, encoding = Encoding.APPLICATION_OCTET_STREAM)
.onFailure { Log.e(TAG, "Error when publishing", it) }
.onSuccess { Log.v(TAG, "Published command!") }
}

Expand Down Expand Up @@ -304,16 +301,12 @@ class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
val arr = ByteArray(mStream.buffer.position())
mStream.buffer.rewind()
mStream.buffer.get(arr)
val payload = Value(arr, Encoding(KnownEncoding.APP_OCTET_STREAM))
val payload = ZBytes.from(arr)

viewModel.zenohSession?.apply {
actionDockKE.intoKeyExpr().onSuccess { ke ->
this.get(ke)
.withValue(payload)
.res().onSuccess {

Log.v(TAG, "Sent dock action publiser : $actionDockKE")
}
actionDockKE.intoSelector().onSuccess { ke ->
this.get(ke, callback = {}, payload, encoding = Encoding.APPLICATION_OCTET_STREAM)
.onSuccess { Log.v(TAG, "Sent dock action publiser : $actionDockKE") }
.onFailure { handleError(TAG, "Failed to send action dock", it) }
}
.onFailure { handleError(TAG, "Failed to parse keyExpr", it) }
Expand All @@ -338,17 +331,14 @@ class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
val arr = ByteArray(mStream.buffer.position())
mStream.buffer.rewind()
mStream.buffer.get(arr)
val payload = Value(arr, Encoding(KnownEncoding.APP_OCTET_STREAM))
val actionSoundKE = mNSEditText.text.toString() + cmdSoundSuffix
viewModel.zenohSession?.apply {
actionSoundKE.intoKeyExpr().onSuccess { ke ->
this.put(ke, payload)
.res().onSuccess {
Log.v(TAG, "Sent sound command")
}
this.put(ke, payload = ZBytes.from(arr), encoding = Encoding.APPLICATION_OCTET_STREAM)
.onSuccess { Log.v(TAG, "Sent sound command") }
.onFailure { handleError(TAG, "Failed to send action dock", it) }
}
.onFailure { handleError(TAG, "Failed to parse keyExpr", it) }
.onFailure { handleError(TAG, "Failed to parse keyExpr", it) }
}

}
Expand All @@ -369,16 +359,16 @@ class TeleopFragment : Fragment(), OnTouchListener, OnSeekBarChangeListener {
viewModel.zenohSession?.apply {
(mNSEditText.text.toString() + batteryStateSuffix).intoKeyExpr().onSuccess { key ->
Log.v(TAG, "Subscribing battery state on: $key")
this.declareSubscriber(key).res().onSuccess { sub ->
this.declareSubscriber(key, Channel()).onSuccess { sub ->
mBatterySubscriber = sub
mBatterySubJob = GlobalScope.launch(Dispatchers.IO) {
sub.receiver?.apply {
sub.receiver.apply {
val iterator = this.iterator()
while (iterator.hasNext()) {
val sample = iterator.next()
Log.v(TAG, "Received data from Zenoh")
val inputStream =
CDRInputStream(ByteBuffer.wrap(sample.value.payload))
CDRInputStream(ByteBuffer.wrap(sample.payload.toBytes()))
Log.v(TAG, "Size from Zenoh is: ${inputStream.buffer.capacity()} - Position: ${inputStream.buffer.position()}")
val battery = Battery(inputStream)
Log.v(TAG, "Battery percentage: ${battery.percentage}")
Expand Down