1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::error::Error;
use crate::frame::QueryType;
use crate::protocol::{Frame, FrameBuilder, ResponseFrame};
use crate::FrameParseError;
use fdcanusb::CanFdFrame;
/// The main struct for interacting with the Moteus.
pub struct Controller<T> {
transport: T,
default_query: FrameBuilder,
/// Disable BRS (Bit Rate Switch) in the CAN FD frames. Useful if your CAN network is unable to perform.
pub disable_brs: bool,
}
#[cfg(feature = "fdcanusb")]
impl Controller<fdcanusb::FdCanUSB> {
/// Create a new [`Controller`] instance with a given transport.
///
/// Currently, the transport is limited to [`FdCanUSB`].
///
/// ```rust
/// # use fdcanusb::{FdCanUSB, serial2};
/// # fn main() -> std::io::Result<()> {
/// moteus::Controller::new(FdCanUSB::open("/dev/fdcanusb")?, false);
/// # Ok(())
/// }
/// ```
pub fn fdcanusb(
path: impl AsRef<std::path::Path>,
disable_brs: bool,
) -> Result<Self, std::io::Error> {
Ok(Self {
transport: fdcanusb::FdCanUSB::open(path)?,
default_query: crate::frame::Query::default().into(),
disable_brs,
})
}
}
impl<T, F> Controller<T>
where
T: crate::transport::Transport<Frame = F>,
F: From<CanFdFrame> + TryInto<ResponseFrame, Error = FrameParseError>,
{
/// Create a new [`Controller`] instance with a given transport.
///
/// Currently, the transport is limited to [`FdCanUSB`].
///
/// ```rust
/// # fn main() -> std::io::Result<()> {
/// moteus::Controller::new(moteus::FdCanUSB::open("/dev/fdcanusb")?, false);
/// # Ok(())
/// # }
/// ```
pub fn new(transport: T, disable_brs: bool) -> Self {
Self {
transport,
default_query: crate::frame::Query::default().into(),
disable_brs,
}
}
/// Creates a new [`Controller`] instance with a custom default query.
///
/// ```rust
/// # use moteus::frame::Query;
/// # use moteus::registers::*;
/// # use moteus::Controller;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let qr = Query::new_with_extra([
/// ControlPosition::read().into(),
/// ControlVelocity::read().into(),
/// ControlTorque::read().into(),
/// ControlPositionError::read().into(),
/// ControlVelocityError::read().into(),
/// ControlTorqueError::read().into(),
/// ]);
/// let mut transport = fdcanusb::FdCanUSB::open("/dev/fdcanusb")?;
/// transport.flush()?;
/// let mut c = Controller::with_query(transport, false, qr);
/// # Ok(())
/// # }
/// ```
pub fn with_query(
transport: T,
disable_brs: bool,
default_query: impl Into<FrameBuilder>,
) -> Self {
Controller {
transport,
default_query: default_query.into(),
disable_brs,
}
}
/// Sends a single query frame to the moteus and returns a [`ResponseFrame`].
///
/// The query frame can be set with [`QueryType`].
/// Use [`QueryType::Default`] to use the default query frame.
/// Use [`QueryType::DefaultAnd`] to merge the default query frame with a custom query frame.
/// Use [`QueryType::Custom`] to use a custom query frame (without the default).
pub fn query(&mut self, id: u8, query: QueryType) -> Result<ResponseFrame, Error<T::Error>> {
let frame = match query {
QueryType::Default => self.default_query.clone().build(),
QueryType::DefaultAnd(q_frame) => self.default_query.clone().merge(q_frame).build(),
QueryType::Custom(q_frame) => q_frame.build(),
};
self.transfer_single_with_response(id, frame)
}
/// Send a single frame to the moteus. No response will be returned.
/// Use [`Controller::send_with_query`] to get a response.
pub fn send_no_response(
&mut self,
id: u8,
frame: impl Into<FrameBuilder>,
) -> Result<(), Error<T::Error>> {
let frame = frame.into().build();
self.transfer_single_no_response(id, frame)
}
/// Sends a single frame with a query to the moteus and returns a [`ResponseFrame`].
///
/// The query frame can be set with [`QueryType`].
/// Use [`QueryType::Default`] to use the default query frame.
/// Use [`QueryType::DefaultAnd`] to merge the default query frame with a custom query frame.
/// Use [`QueryType::Custom`] to use a custom query frame (without the default).
pub fn send_with_query(
&mut self,
id: u8,
frame: impl Into<FrameBuilder>,
query: QueryType,
) -> Result<ResponseFrame, Error<T::Error>> {
let frame = match query {
QueryType::Default => frame.into().merge(self.default_query.clone()).build(),
QueryType::DefaultAnd(q_frame) => frame
.into()
.merge(self.default_query.clone())
.merge(q_frame)
.build(),
QueryType::Custom(q_frame) => frame.into().merge(q_frame).build(),
};
self.transfer_single_with_response(id, frame)
}
fn transfer_single_no_response(
&mut self,
id: u8,
frame: impl Into<Frame>,
) -> Result<(), Error<T::Error>> {
let frame = frame.into();
let arbitration_id = id as u16;
let frame = CanFdFrame {
arbitration_id,
data: frame.as_bytes()?,
brs: Some(!self.disable_brs),
..Default::default()
};
self.transport.transmit(frame.into())?;
Ok(())
}
fn transfer_single_with_response(
&mut self,
id: u8,
frame: impl Into<Frame>,
) -> Result<ResponseFrame, Error<T::Error>> {
let frame = frame.into();
let arbitration_id = id as u16 | 0x8000;
let frame = CanFdFrame {
arbitration_id,
data: frame.as_bytes()?,
brs: Some(!self.disable_brs),
..Default::default()
};
self.transport.transmit(frame.into())?;
let response = self.transport.receive()?;
Ok(response.try_into()?)
}
}