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

feat: Add Dora-kit car Control in node-hub #715

Merged
merged 9 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 82 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"node-hub/dora-rerun",
"node-hub/terminal-print",
"node-hub/openai-proxy-server",
"node-hub/dora-chongyoucar",
"libraries/extensions/ros2-bridge",
"libraries/extensions/ros2-bridge/msg-gen",
"libraries/extensions/ros2-bridge/python",
Expand Down
2 changes: 2 additions & 0 deletions node-hub/dora-chongyoucar/.env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Defining the serial port number
SERIAL_PORT = "/dev/ttyUSB0"
18 changes: 18 additions & 0 deletions node-hub/dora-chongyoucar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "dora-chongyoucar"
edition = "2021"
version.workspace = true
description.workspace = true
documentation.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
eyre = "0.6.8"
dora-node-api = { workspace = true, features = ["tracing"] }
dotenv = "0.15.0"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
serial = "0.4.0"
thiserror = "1.0.63"
tokio = { version = "1.24.2", features = ["rt", "macros", "rt-multi-thread"] }
15 changes: 15 additions & 0 deletions node-hub/dora-chongyoucar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Chongyou Car Control

## Introduce

Control of the movement of the trolley by receiving texts

## Text Command Description

|`text`|`description`|
|---|---|
|`forward`|Control the trolley to move forward|
|`left`|Control the trolley to move left|
|`right`|Control the trolley to move right|
|`backward`|Control the trolley to move backward|
|`stop`|Control the trolley to move stop|
35 changes: 35 additions & 0 deletions node-hub/dora-chongyoucar/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 差速小车
pub fn send_speed_to_x4chassis(x: f64, y: f64, w: f64) -> Vec<u8> {

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused variable: `y`

Check warning on line 2 in node-hub/dora-chongyoucar/src/command.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused variable: `y`
let mut data = vec![];

let speed_offset = 10.0; // 速度偏移值 10m/s,把速度转换成正数发送

data.push(0xAE_u8);
data.push(0xEA);
data.push(0x0B);
data.push(0xF3);
let x = ((x + speed_offset) * 100.0) as u16;
data.push((x >> 8) as u8);
data.push(x as u8);
data.push(0x00);
data.push(0x00);
let w = ((w + speed_offset) * 100.0) as u16;
data.push((w >> 8) as u8);
data.push(w as u8);
data.push(0x00);
data.push(0x00);
let len = data.len();
data[2] = len as u8 - 1;

let mut count = 0;
for &d in data.iter().take(len).skip(2) {
count += d as u16;
}
// 数据校验位
data.push(count as u8);

data.push(0xEF);
data.push(0xFE);

data
}
2 changes: 2 additions & 0 deletions node-hub/dora-chongyoucar/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// serial port
pub const SERIAL_PORT: &str = "SERIAL_PORT";
8 changes: 8 additions & 0 deletions node-hub/dora-chongyoucar/src/enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

// command type
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "command_type")]
pub enum CommandType {
DifferSpeed { x: f64, y: f64, w: f64 }, // Differential Speed
}
11 changes: 11 additions & 0 deletions node-hub/dora-chongyoucar/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("serial connect fail")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this error more descriptive. Can we add the serial port used?

SerialConnect,
#[error("serial settings set fail")]
SerialSettingsSet,
#[error("serial set timeout fail")]
SerialSetTimeout,
}
9 changes: 9 additions & 0 deletions node-hub/dora-chongyoucar/src/json_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};

use crate::enums::CommandType;

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonData {
pub sleep_second: u64,
pub command: CommandType,
}
88 changes: 88 additions & 0 deletions node-hub/dora-chongyoucar/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Chongyou Car Control
// Author:Leon(李扬)

mod command;
mod config;
mod enums;
mod error;
mod json_data;

use std::{io::Write, time::Duration};

use dora_node_api::{DoraNode, Event};
use error::Error;
use serial::SerialPort;
use tokio::sync::mpsc;

#[tokio::main]
async fn main() -> eyre::Result<()> {
dotenv::dotenv().ok();

// serial port
let serial_port = std::env::var(config::SERIAL_PORT).unwrap_or("/dev/ttyUSB0".to_string());
let speed = std::env::var("SPEED")
.unwrap_or("0.2".to_string())
.parse::<f64>()
.unwrap_or(0.2_f64);

// connect serial
const COM_SETTINGS: serial::PortSettings = serial::PortSettings {
baud_rate: serial::Baud115200,
char_size: serial::Bits8,
parity: serial::ParityNone,
stop_bits: serial::Stop1,
flow_control: serial::FlowNone,
};

let mut com = serial::open(&serial_port).map_err(|_| Error::SerialConnect)?;
com.configure(&COM_SETTINGS)
.map_err(|_| Error::SerialSettingsSet)?;
com.set_timeout(Duration::from_millis(1000))
.map_err(|_| Error::SerialSetTimeout)?;

// msg channel
let (tx_key, mut rx_key) = mpsc::channel::<(f64, f64)>(100);

tokio::spawn(async move {
while let Some((x, w)) = rx_key.recv().await {
// println!("{:?}", (x, w));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove comments.

let data = command::send_speed_to_x4chassis(x, 0.0, w);
com.write_all(&data).ok();
}
});

let r = 1.0;

let (_, mut events) = DoraNode::init_from_env()?;

while let Some(event) = events.recv() {
if let Event::Input {
id: _,
metadata: _,
data,
} = event
{
let received_string: &str = TryFrom::try_from(&data).unwrap();
match received_string {
"forward" => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a float64 array as input instead of string so that it's easier to configure later.

It's also a more scientific representation of the action.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll readjust the technical program.

tx_key.send((speed * r, 0.0)).await.ok();
}
"left" => {
tx_key.send((0.0, speed * r)).await.ok();
}
"right" => {
tx_key.send((0.0, -speed * r)).await.ok();
}
"backward" => {
tx_key.send((-speed * r, 0.0)).await.ok();
}
"stop" => {
tx_key.send((0.0, 0.0)).await.ok();
}
_ => {}
}
}
}

Ok(())
}
Loading