Skip to content
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ add_executable(
setup/cli/commands/change_motor_id_commands.cpp
setup/cli/commands/write_motor_param_commands.cpp
setup/cli/commands/clear_error_commands.cpp
setup/cli/commands/diagnose_commands.cpp)
setup/cli/commands/diagnose_commands.cpp
setup/cli/commands/param_io.cpp)
target_include_directories(
openarm-can-cli PRIVATE setup/cli ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(openarm-can-cli PRIVATE openarm_can CLI11::CLI11)
Expand Down
24 changes: 23 additions & 1 deletion setup/cli/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

#include <cstdint>
#include <iomanip>
#include <optional>
#include <sstream>
#include <string>
#include <vector>

namespace openarm::canbus {
class CANSocket;
}

namespace openarm::cli {

// ========================================================================
Expand Down Expand Up @@ -53,7 +58,8 @@ int run_discover(const std::string& interface, int max_id, bool full_scan = fals
int run_change_id(const std::string& interface, int current_id, int new_slave_id, int new_master_id,
bool save);

int run_change_baud(const std::string& interface, int baudrate, int canid, bool flash);
int run_change_baud(const std::string& interface, int baudrate,
const std::vector<std::string>& ids_str, bool flash);

int run_read_params(const std::string& interface, bool use_arm_ids,
const std::vector<std::string>& custom_ids_str);
Expand Down Expand Up @@ -84,6 +90,22 @@ int run_diagnose(const std::string& interface, bool use_arm_ids,
// [ Shared Utilities ]
// ========================================================================

// Damiao parameter access through the 0x7FF config ID. Unlike a bare write,
// each call waits for the motor's reply, so a dead or mismatched motor is
// reported instead of silently ignored. The socket must have CAN FD enabled:
// on an FD bus the motors reply with FD frames.
std::optional<uint32_t> param_read(canbus::CANSocket& socket, uint16_t slave_id, uint8_t rid);
// Writes and reads the value back; false unless the motor holds it.
bool param_write(canbus::CANSocket& socket, uint16_t slave_id, uint8_t rid, uint32_t value);
// Only checks the acknowledgement. For a value that changes how the motor
// talks, such as can_br: it switches right after acknowledging, so a read-back
// on the old settings gets no reply.
bool param_write_acked(canbus::CANSocket& socket, uint16_t slave_id, uint8_t rid, uint32_t value);
bool motor_disable(canbus::CANSocket& socket, uint16_t slave_id);
// Disables the motor and saves its RAM parameters to flash. The caller
// verifies the result with param_read.
bool param_save(canbus::CANSocket& socket, uint16_t slave_id);

inline std::string format_hex_id(uint32_t id) {
std::stringstream ss;
ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << id;
Expand Down
4 changes: 4 additions & 0 deletions setup/cli/commands/can_configure_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ int run_can_configure(const std::vector<std::string>& interfaces,
if (options.fd_mode) {
cmd_set += " dbitrate " + std::to_string(options.dbitrate) + " fd on dsample-point " +
options.dsample_point + " dsjw " + options.dsjw;
} else {
// The kernel keeps the previous fd setting unless told otherwise,
// so leaving this out does not switch an FD interface back.
cmd_set += " fd off";
}

std::cout << " " << cmd_set << std::endl;
Expand Down
232 changes: 175 additions & 57 deletions setup/cli/commands/change_motor_baudrate_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,45 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <linux/can.h>

#include <chrono>
#include <iostream>
#include <map>
#include <openarm/canbus/can_socket.hpp>
#include <thread>
#include <openarm/damiao_motor/dm_motor_constants.hpp>
#include <string>
#include <vector>

#include "cli.hpp"

namespace openarm::cli {

int run_change_baud(const std::string& interface, int baudrate, int canid, bool flash) {
namespace {

struct MotorResult {
uint16_t id;
std::string status;
bool ok;
};

void print_results(const std::vector<MotorResult>& results) {
std::cout << "---------------------------------------------------------" << std::endl;
for (const auto& r : results) {
std::cout << " " << (r.ok ? "✓" : "✗") << " Motor " << format_hex_id(r.id) << ": "
<< r.status << std::endl;
}
std::cout << "---------------------------------------------------------" << std::endl;
}

} // namespace

// The motor switches to the new baudrate right after acknowledging the write,
// so the sequence is: confirm every motor answers, write all of them on the
// current interface settings, move the interface to the new baudrate, confirm
// every motor answers there, and only then save. A failure before the save
// leaves flash untouched, and a power cycle returns every motor to where it
// started.
int run_change_baud(const std::string& interface, int baudrate,
const std::vector<std::string>& ids_str, bool flash) {
// Mapping of baudrate values to DaMiao register codes
static const std::map<int, uint8_t> BAUDRATE_MAP = {
{125000, 0}, {200000, 1}, {250000, 2}, {500000, 3}, {1000000, 4}, {2000000, 5},
Expand All @@ -35,33 +61,128 @@ int run_change_baud(const std::string& interface, int baudrate, int canid, bool
return 1;
}

std::vector<uint16_t> ids;
for (const auto& id_str : ids_str) {
try {
ids.push_back(static_cast<uint16_t>(std::stoul(id_str, nullptr, 0)));
} catch (...) {
std::cerr << "✗ Error: Invalid ID format '" << id_str << "'\n";
return 1;
}
}
if (ids.empty()) {
std::cerr << "✗ Error: No target motor IDs specified.\n";
return 1;
}

const uint8_t code = BAUDRATE_MAP.at(baudrate);
const uint8_t rid = static_cast<uint8_t>(damiao_motor::RID::can_br);

// Up to 1 Mbps the motor runs Classic CAN; above it, CAN FD with a 1 Mbps
// arbitration phase.
CanConfigureOptions target;
target.fd_mode = baudrate > 1000000;
target.bitrate = target.fd_mode ? 1000000 : baudrate;
target.dbitrate = baudrate;

std::vector<uint16_t> to_change;

try {
std::cout << "Connecting to " << interface << " (Classic CAN mode)..." << std::endl;
openarm::canbus::CANSocket socket(interface, false);

std::cout << "---------------------------------------------------------" << std::endl;

// --- Step 1: Write New Baudrate ---
std::cout << "[1/2] Writing baudrate " << baudrate << " to Motor ID " << canid << "..."
<< std::endl;

struct can_frame frame;
frame.can_id = 0x7FF; // System Configuration ID
frame.can_dlc = 8;
frame.data[0] = canid & 0xFF;
frame.data[1] = (canid >> 8) & 0xFF;
frame.data[2] = 0x55; // Command: Write Parameter
frame.data[3] = 0x23; // Register: RID_BAUD (35)
frame.data[4] = BAUDRATE_MAP.at(baudrate);
for (int i = 5; i < 8; i++) frame.data[i] = 0x00;

if (!socket.write_can_frame(frame)) {
std::cerr << "✗ Error: Failed to write CAN frame." << std::endl;
// FD enabled so that the motors' replies are received on an FD bus.
openarm::canbus::CANSocket socket(interface, true);

// --- Step 1: Every motor must answer before anything is written ---
std::cout << "[1/5] Checking motors on " << interface << "..." << std::endl;
std::vector<MotorResult> results;
bool all_answer = true;
for (uint16_t id : ids) {
auto current = param_read(socket, id, rid);
if (!current) {
results.push_back({id, "no answer", false});
all_answer = false;
} else if (*current == code) {
results.push_back({id, "already can_br=" + std::to_string(code), true});
} else {
results.push_back(
{id, "can_br=" + std::to_string(*current) + " -> " + std::to_string(code),
true});
to_change.push_back(id);
}
}
print_results(results);
if (!all_answer) {
std::cerr << "✗ Not every motor answers. Nothing was written.\n"
<< " Check the IDs, power, and that the interface matches the motors' "
"current baudrate (see 'discover')."
<< std::endl;
return 1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
if (to_change.empty() && !flash) {
std::cout << "✓ All motors already at " << baudrate << " bps. Nothing to do."
<< std::endl;
return 0;
}

// --- Step 2: Flash Save Operation ---
// --- Step 2: Write on the current settings ---
// Only the acknowledgement can be checked here: it is the last frame
// the motor sends at its old baudrate.
if (!to_change.empty()) {
std::cout << "[2/5] Writing can_br=" << static_cast<int>(code) << "..." << std::endl;
for (uint16_t id : to_change) {
if (!motor_disable(socket, id) || !param_write_acked(socket, id, rid, code)) {
std::cerr << "✗ Motor " << format_hex_id(id)
<< " did not acknowledge. Flash was not written.\n"
<< " Power cycle the motors to return them to their saved "
"baudrate."
<< std::endl;
return 1;
}
std::cout << " ✓ Motor " << format_hex_id(id) << " acknowledged" << std::endl;
}
}
} catch (const std::exception& e) {
std::cerr << "✗ Exception: " << e.what() << std::endl;
return 1;
}

// --- Step 3: Follow the motors to the new baudrate ---
if (!to_change.empty()) {
std::cout << "[3/5] Moving " << interface << " to " << baudrate << " bps..." << std::endl;
if (run_can_configure({interface}, target) != 0) {
std::cerr << "✗ Failed to reconfigure " << interface << ". Flash was not written.\n"
<< " Power cycle the motors to return them to their saved baudrate."
<< std::endl;
return 1;
}
}

try {
// A new socket: the old one was bound across the interface going down.
openarm::canbus::CANSocket socket(interface, true);

// --- Step 4: Every motor must answer at the new baudrate ---
std::cout << "[4/5] Verifying at " << baudrate << " bps..." << std::endl;
std::vector<MotorResult> results;
bool all_ok = true;
for (uint16_t id : ids) {
auto value = param_read(socket, id, rid);
if (value && *value == code) {
results.push_back({id, "answers with can_br=" + std::to_string(code), true});
} else {
results.push_back(
{id, value ? "reads can_br=" + std::to_string(*value) : "no answer", false});
all_ok = false;
}
}
print_results(results);
if (!all_ok) {
std::cerr << "✗ Not every motor answers at the new baudrate. Flash was not written.\n"
<< " Power cycle the motors to return them to their saved baudrate."
<< std::endl;
return 1;
}

// --- Step 5: Flash Save Operation ---
if (flash) {
std::cout << "---------------------------------------------------------" << std::endl;
std::cout << "[!] WARNING: FLASH WRITE OPERATION" << std::endl;
Expand All @@ -72,42 +193,39 @@ int run_change_baud(const std::string& interface, int baudrate, int canid, bool
<< std::endl;
std::cout << "---------------------------------------------------------" << std::endl;

std::cout << "[2/2] Executing Flash Save..." << std::endl;

// A. Disable motor (Mandatory for Flash Save)
struct can_frame dis;
dis.can_id = canid;
dis.can_dlc = 8;
for (int i = 0; i < 7; i++) dis.data[i] = 0xFF;
dis.data[7] = 0xFD; // Disable command
socket.write_can_frame(dis);
std::this_thread::sleep_for(std::chrono::milliseconds(50));

// B. Send Save Command
struct can_frame sv;
sv.can_id = 0x7FF;
sv.can_dlc = 8;
sv.data[0] = canid & 0xFF;
sv.data[1] = (canid >> 8) & 0xFF;
sv.data[2] = 0xAA; // Command: Save to Flash
for (int i = 3; i < 8; i++) sv.data[i] = 0x00;

if (socket.write_can_frame(sv)) {
std::this_thread::sleep_for(std::chrono::milliseconds(40));
std::cout << "✓ Parameters saved to Flash successfully." << std::endl;
std::cout << "[5/5] Executing Flash Save..." << std::endl;
results.clear();
for (uint16_t id : ids) {
auto value = param_save(socket, id) ? param_read(socket, id, rid) : std::nullopt;
if (value && *value == code) {
results.push_back({id, "saved", true});
} else {
results.push_back({id, "no answer after save", false});
all_ok = false;
}
}
print_results(results);
if (!all_ok) {
std::cerr << "✗ Some motors did not answer after the save. Power cycle and run "
"this command again without --save to see where they stand."
<< std::endl;
return 1;
}
}

std::cout << "---------------------------------------------------------" << std::endl;
std::cout << "✓ Baudrate Change Sequence Completed." << std::endl;
std::cout << "[!] POWER CYCLE REQUIRED to apply changes permanently." << std::endl;
std::cout << "---------------------------------------------------------" << std::endl;

return 0;
} catch (const std::exception& e) {
std::cerr << "✗ Exception: " << e.what() << std::endl;
return 1;
}

std::cout << "✓ All motors run at " << baudrate << " bps." << std::endl;
if (flash) {
std::cout << "[!] Power cycle, then run this command again without --save to confirm\n"
" the motors come back at "
<< baudrate << " bps." << std::endl;
} else {
std::cout << "[!] Not saved (--save not given): a power cycle reverts them." << std::endl;
}
return 0;
}

} // namespace openarm::cli
3 changes: 3 additions & 0 deletions setup/cli/commands/discover_motor_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ bool reconfigure_can_interface(const std::string& iface, int br, int dbr) {

if (br != dbr) {
cmd += " dbitrate " + std::to_string(dbr) + " fd on dsample-point 0.60 dsjw 1";
} else {
// Otherwise the interface stays in FD mode from the previous step.
cmd += " fd off";
}
cmd += " restart-ms 100 2>/dev/null";

Expand Down
Loading
Loading