Skip to content

Commit

Permalink
Add code to ping motors on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
abnv-goyal committed Nov 22, 2024
1 parent 8c5bcbe commit 402c2cd
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/utils/motorPing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "../CAN/CAN.h"
#include "../CAN/CANUtils.h"
#include "../world_interface/real_world_constants.h"

#include <chrono>
#include <thread>

namespace motorPing {
bool hasData(deviceid_t id) {
auto start = std::chrono::steady_clock::now();
const int timeout_ms = 100;

while (std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count() < timeout_ms) {
if (can::checkDeviceTelemetry(id)) {
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return false;
}

void pingMotors() {
for (auto motorMap : robot::motorSerialIDMap) {
can::pullDeviceTelemetry(std::make_pair(devicegroup_t::motor, motorMap.second),
telemtype_t::voltage);
if (!hasData(std::make_pair(devicegroup_t::motor, motorMap.second))) {
LOG_F(ERROR, "Motor not connected!\nID: " + motorMap.first +
"\nSerial: " + motorMap.second);
throw std::runtime_error("Motor not connected!\nID: " + motorMap.first +
"\nSerial: " + motorMap.second);
}
}
}
} // namespace motorPing

0 comments on commit 402c2cd

Please sign in to comment.