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

Add: Device module and example for Ping1D #23

Merged

Conversation

RaulTrombin
Copy link
Member

No description provided.

@RaulTrombin RaulTrombin marked this pull request as draft April 18, 2024 17:17
@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch 4 times, most recently from 67d02f2 to db893ec Compare April 22, 2024 22:56
@RaulTrombin RaulTrombin marked this pull request as ready for review April 22, 2024 22:58
@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch 5 times, most recently from b4cb7aa to c614a09 Compare April 24, 2024 16:08
@RaulTrombin RaulTrombin marked this pull request as draft April 24, 2024 16:11
@RaulTrombin RaulTrombin changed the title Add device module and example [WIP] Add: Device module and example for Ping1D Apr 24, 2024
@RaulTrombin RaulTrombin marked this pull request as ready for review April 24, 2024 16:11
src/device.rs Outdated
Comment on lines 56 to 76
async fn stream(
mut serial_stream: SplitStream<Framed<SerialStream, PingCodec>>,
broadcast_tx: Sender<ProtocolMessage>,
) {
'serial_stream: loop {
while let Some(msg) = serial_stream.next().await {
match msg {
Ok(msg) => {
if let Err(_e) = broadcast_tx.send(msg) {
dbg! {_e};
break 'serial_stream;
};
}
Err(e) => {
dbg!(e);
}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This task is likely to leave forever, as the only way out of the loop is if the broadcast_tx fails to send (by being closed), but who closes it?

Copy link
Member Author

@RaulTrombin RaulTrombin Apr 25, 2024

Choose a reason for hiding this comment

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

Is there a master "panic!" for this use case?
I believe if it get some faltal error on serial bus it should stop.
We can create a manager which retry stabilish the port, but I not but the port creation inside the device yet.

Ok(())
}

mod cli_args {
Copy link
Member

Choose a reason for hiding this comment

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

use structs over clap builder pattern, it's simpler and easier to read/maintain.

Copy link
Member Author

Choose a reason for hiding this comment

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

k, please check the newer version


let mut port = tokio_serial::new(port_name, baud_rate).open_native_async()?;
#[cfg(unix)]
port.set_exclusive(false)?;
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for this ? Is missing a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe it's to allow reuse the ports in the same scope,
https://docs.rs/tokio-serial/latest/tokio_serial/struct.SerialStream.html#method.set_exclusive

It was into example tokio_serial, still works without it, let remove and keep it to future if it's required

}
Err(_e) => break,
}
if ProfileStructVector.len() == 30 {
Copy link
Member

Choose a reason for hiding this comment

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

>=30

src/device.rs Outdated
Comment on lines 150 to 151
if !(common::AckStruct::id() == answer.message_id
|| common::NackStruct::id() == answer.message_id)
Copy link
Member

Choose a reason for hiding this comment

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

common::AckStruct::id() != answer.message_id && common::NackStruct::id() != answer.message_id

src/device.rs Outdated
if answer.nacked_id != message_id {
continue;
};
return Err(PingError::NackError(answer));
Copy link
Member

Choose a reason for hiding this comment

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

The answer here is unnecessary, there is not useful inside

Copy link
Member Author

@RaulTrombin RaulTrombin Apr 25, 2024

Choose a reason for hiding this comment

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

For logging it could be usefull, since it have the request id inside it.
Should we log and return error? I think a generic error will be harder to track

Copy link
Member

Choose a reason for hiding this comment

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

Since Nack contains the error contains nack_message https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#2-nack it's better to return the message itself or the struct.
Err(PingError::NackError(String))

};
return Err(PingError::NackError(answer));
}
_ => return Err(PingError::TryFromError(answer)), // Almost unreachable, but raises error ProtocolMessage
Copy link
Member

Choose a reason for hiding this comment

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

Shoudnt this be a continue ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we never reach this condition. If it's an Ack or Nack, it verifies the ID.
This is just in case any error happens during conversion (which will be almost impossible, as we do CRC during the message reception.)

src/device.rs Outdated
};

match tokio::time::timeout(tokio::time::Duration::from_secs(15), future).await {
Ok(_result) => Ok(()),
Copy link
Member

Choose a reason for hiding this comment

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

This should return the future result, not an empty ok

src/device.rs Outdated
match msg {
Ok(msg) => {
if let Err(_e) = broadcast_tx.send(msg) {
dbg! {_e};
Copy link
Member

Choose a reason for hiding this comment

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

This should use parentheses not braces

Copy link
Member Author

Choose a reason for hiding this comment

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

changed to tracing, since we merged last related PR

src/device.rs Outdated Show resolved Hide resolved
examples/ping_1d.rs Outdated Show resolved Hide resolved
@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch from c614a09 to fbb3147 Compare April 25, 2024 14:58
@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch 2 times, most recently from b70f6df to 07d42bf Compare April 25, 2024 15:01
src/device.rs Outdated
if answer.acked_id != message_id {
continue;
};
return Ok(answer);
Copy link
Member

Choose a reason for hiding this comment

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

In this case, we should return an empty Ok since ack will always contain message_id
https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#1-ack

src/device.rs Outdated
if answer.nacked_id != message_id {
continue;
};
return Err(PingError::NackError(answer));
Copy link
Member

Choose a reason for hiding this comment

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

Since Nack contains the error contains nack_message https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#2-nack it's better to return the message itself or the struct.
Err(PingError::NackError(String))

@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch from 07d42bf to 3c7b360 Compare April 25, 2024 18:28
@patrickelectric patrickelectric force-pushed the creave_device_inherit branch 2 times, most recently from d2af646 to 9f9861b Compare April 25, 2024 18:50
@RaulTrombin RaulTrombin force-pushed the creave_device_inherit branch from 9f9861b to 373a6ec Compare April 25, 2024 19:02
@RaulTrombin
Copy link
Member Author

@patrickelectric with you

@patrickelectric patrickelectric merged commit 68203e7 into bluerobotics:master Apr 25, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants