Skip to content

Commit 4802e35

Browse files
committed
Broken code
1 parent eec7b8e commit 4802e35

4 files changed

Lines changed: 29 additions & 15 deletions

File tree

src/CAN/CAN.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace {
4343
// time to sleep after getting a CAN read error
4444
constexpr std::chrono::milliseconds READ_ERR_SLEEP(100);
4545
constexpr std::chrono::milliseconds ACK_TIMEOUT(50);
46+
constexpr std::chrono::milliseconds READ_TIMEOUT(100);
4647
// CAN26 11-bit ID layout: [priority:1][deviceUUID:7][peripheral:1][power:1][motor:1]
4748
// Match on UUID field to filter for packets addressed to this device
4849
constexpr uint32_t CAN_MASK = 0x3F8; // UUID field
@@ -66,7 +67,7 @@ std::unordered_map<
6667
std::shared_mutex directReadMapMutex;
6768
std::unordered_map<
6869
std::pair<CANDeviceUUID_t, uint16_t>,
69-
std::function<void(CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>> directReadCallbackMap;
70+
std::pair<std::function<void(CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread>> directReadCallbackMap;
7071

7172
// Endpoint JSONs
7273
nlohmann::json pro_endpoints;
@@ -133,7 +134,7 @@ void handleDirectRead(CANPacket_t& packet) {
133134
// Unlock in case callback wants to modify the map?
134135
mapReadLock.unlock();
135136
if (it != directReadCallbackMap.end()) {
136-
it->second(decoded);
137+
it->second.first(decoded);
137138
}
138139
}
139140

@@ -362,7 +363,17 @@ void addDirectReadCallback(CANDevice_t device, uint16_t endpoint, const std::fun
362363
LOG_F(WARNING, "Callback already exists for 0x%x endpoint %d! Ignoring..", device.deviceUUID, endpoint);
363364
return;
364365
}
365-
directReadCallbackMap.insert({key, callback});
366+
std::thread timeout([device, endpoint]() {
367+
std::this_thread::sleep_for(READ_TIMEOUT);
368+
LOG_F(ERROR, "0x%x read of %d timed out!", device.deviceUUID, endpoint);
369+
});
370+
auto element = std::make_pair<const std::function<void (CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread>(callback, timeout);
371+
372+
// std::unordered_map<
373+
// std::pair<CANDeviceUUID_t, uint16_t>,
374+
// std::pair<std::function<void(CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread>> directReadCallbackMap;
375+
376+
directReadCallbackMap.insert({key, element});
366377
}
367378

368379
void removeDirectReadCallback(CANDevice_t device, uint16_t endpoint) {

src/CAN/CANBoard.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ CANBoard::CANBoard(robot::types::boardid_t board_id, CANDevice_t device)
4747
if (auto it = robot::boardInversionMap.find(board_id); it != robot::boardInversionMap.end()) {
4848
this->inversion_factor = it->second;
4949
}
50+
51+
// Wait until configs are grabbed
52+
auto start = std::chrono::system_clock::now();
53+
while (!this->vel_limit);
54+
auto end = std::chrono::system_clock::now();
55+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
56+
LOG_F(INFO, "0x%x took %ld ms", this->device.deviceUUID, elapsed.count());
5057
}
5158

5259
if (device.peripheralDomain) {
@@ -70,24 +77,21 @@ void CANBoard::setMotorPower(double power) {
7077
if (this->watchdog) {
7178
this->setMotorState(can::motor::axis_state_t::idle);
7279

73-
// hang until this actually goes idle for funsies
74-
this->correct = false;
7580
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.current_state"); endpoint != nullptr) {
7681
uint16_t endpoint_id = endpoint["id"];
7782
addDirectReadCallback(this->device, endpoint_id, [this, endpoint_id](auto decoded) {
7883
if (decoded.value_uint8 != static_cast<uint8_t>(can::motor::axis_state_t::idle)) {
7984
LOG_F(ERROR, "0x%x DID NOT LISTEN AND IS NOT IDLE", this->device.deviceUUID);
8085
this->setMotorState(can::motor::axis_state_t::idle);
86+
this->read(endpoint_id);
8187
} else {
8288
removeDirectReadCallback(this->device, endpoint_id);
83-
correct = true;
8489
}
8590
});
8691

8792
this->read(endpoint_id);
8893
}
8994

90-
while (!this->correct);
9195
} else if (this->board_id == robot::types::boardid_t::shoulder || this->board_id == robot::types::boardid_t::elbow) {
9296
// Set motor general lockin vel to 0
9397
this->setMotorState(can::motor::axis_state_t::lockin_spin);

src/CAN/CANBoard.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class CANBoard {
4545
// Estimates received
4646
std::shared_mutex board_mutex;
4747
robot::types::DataPoint<int32_t> position_mdeg;
48-
49-
// debug
50-
bool correct = false;
5148
};
5249

5350
} // namespace can

src/CAN/FakeCANBoard.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ int main() {
6161

6262
while (true) {
6363
if (testMode == TestMode::State) {
64-
std::stringstream state_msg("Enter desired motor state:\n");
65-
state_msg << static_cast<uint8_t>(can::motor::axis_state_t::idle) << " idle\n";
66-
state_msg << static_cast<uint8_t>(can::motor::axis_state_t::closed_loop_control) << " closed loop control\n";
64+
std::stringstream state_msg("");
65+
state_msg << "Enter desired motor state:\n";
66+
state_msg << static_cast<int>(can::motor::axis_state_t::idle) << " idle\n";
67+
state_msg << static_cast<int>(can::motor::axis_state_t::closed_loop_control) << " closed loop control\n";
6768

6869
int state = prompt(state_msg.str().c_str());
6970
can::motor::axis_state_t motor_state = static_cast<can::motor::axis_state_t>(state);
@@ -73,9 +74,10 @@ int main() {
7374
if (nlohmann::json endpoint = can::getEndpoint(board->getBoardID(), "axis0.current_state"); endpoint != nullptr) {
7475
uint16_t endpoint_id = endpoint["id"];
7576
can::addDirectReadCallback(board->getDevice(), endpoint_id, [board, motor_state, endpoint_id](auto decoded) {
76-
if (decoded.value_uint8 != static_cast<uint8_t>(can::motor::axis_state_t::idle)) {
77-
LOG_F(ERROR, "0x%x DID NOT LISTEN AND IS NOT STATE %d", board->getDevice().deviceUUID, motor_state);
77+
if (decoded.value_uint8 != static_cast<uint8_t>(motor_state)) {
78+
LOG_F(ERROR, "0x%x DID NOT LISTEN AND IS NOT STATE %d", board->getDevice().deviceUUID, static_cast<uint8_t>(motor_state));
7879
board->setMotorState(motor_state);
80+
board->read(endpoint_id);
7981
} else {
8082
can::removeDirectReadCallback(board->getDevice(), endpoint_id);
8183
correct = true;

0 commit comments

Comments
 (0)