Add additional methods for low level single motor control (#22)

This PR adds the ability to refresh and query specific motors. The
current use case is primarily for calibration.
This commit is contained in:
thomason 2025-09-01 05:57:37 +09:00 committed by GitHub
parent 4478a105d8
commit ebfbf010c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 43 additions and 7 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
# Copyright 2025 Enactic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
/.vscode
/build

View File

@ -27,9 +27,9 @@ public:
ArmComponent(canbus::CANSocket& can_socket);
~ArmComponent() = default;
void init_motor_devices(std::vector<damiao_motor::MotorType> motor_types,
std::vector<uint32_t> send_can_ids, std::vector<uint32_t> recv_can_ids,
bool use_fd);
void init_motor_devices(const std::vector<damiao_motor::MotorType>& motor_types,
const std::vector<uint32_t>& send_can_ids,
const std::vector<uint32_t>& recv_can_ids, bool use_fd);
private:
std::vector<damiao_motor::Motor> motors_;

View File

@ -48,6 +48,8 @@ public:
void disable_all();
void set_zero_all();
void refresh_all();
void refresh_one(int i);
// The timeout for reading from socket, set to timeout_us.
// Tuning this value may improve the performance but should be done with caution.
void recv_all(int timeout_us = 500);

View File

@ -32,9 +32,12 @@ public:
// Common motor operations
void enable_all();
void disable_all();
void set_zero_all();
void set_callback_mode_all(CallbackMode callback_mode);
// Flash new zero position
void set_zero(int i);
void set_zero_all();
// Refresh operations (for individual motors)
void refresh_one(int i);
void refresh_all();
@ -49,6 +52,7 @@ public:
// Device collection access
std::vector<Motor> get_motors() const;
Motor get_motor(int i) const;
canbus::CANDeviceCollection& get_device_collection() { return *device_collection_; }
protected:

View File

@ -23,9 +23,9 @@ namespace openarm::can::socket {
ArmComponent::ArmComponent(canbus::CANSocket& can_socket)
: damiao_motor::DMDeviceCollection(can_socket) {}
void ArmComponent::init_motor_devices(std::vector<damiao_motor::MotorType> motor_types,
std::vector<canid_t> send_can_ids,
std::vector<canid_t> recv_can_ids, bool use_fd) {
void ArmComponent::init_motor_devices(const std::vector<damiao_motor::MotorType>& motor_types,
const std::vector<canid_t>& send_can_ids,
const std::vector<canid_t>& recv_can_ids, bool use_fd) {
// Reserve space to prevent vector reallocation that would invalidate motor
// references
motors_.reserve(motor_types.size());

View File

@ -73,6 +73,12 @@ void OpenArm::refresh_all() {
}
}
void OpenArm::refresh_one(int i) {
for (damiao_motor::DMDeviceCollection* device_collection : sub_dm_device_collections_) {
device_collection->refresh_one(i);
}
}
void OpenArm::disable_all() {
for (damiao_motor::DMDeviceCollection* device_collection : sub_dm_device_collections_) {
device_collection->disable_all();

View File

@ -41,6 +41,12 @@ void DMDeviceCollection::disable_all() {
}
}
void DMDeviceCollection::set_zero(int i) {
auto dm_device = get_dm_devices().at(i);
auto zero_packet = CanPacketEncoder::create_set_zero_command(dm_device->get_motor());
send_command_to_device(dm_device, zero_packet);
}
void DMDeviceCollection::set_zero_all() {
for (auto dm_device : get_dm_devices()) {
CANPacket zero_packet = CanPacketEncoder::create_set_zero_command(dm_device->get_motor());
@ -114,6 +120,8 @@ std::vector<Motor> DMDeviceCollection::get_motors() const {
return motors;
}
Motor DMDeviceCollection::get_motor(int i) const { return get_dm_devices().at(i)->get_motor(); }
std::vector<std::shared_ptr<DMCANDevice>> DMDeviceCollection::get_dm_devices() const {
std::vector<std::shared_ptr<DMCANDevice>> dm_devices;
for (const auto& [id, device] : device_collection_->get_devices()) {