Skip to content

Commit 5452d5f

Browse files
committed
Fix deadlock
1 parent 472ae22 commit 5452d5f

4 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/CAN/CAN.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ void handleDirectRead(CANPacket_t& packet) {
270270
// Read access
271271
std::shared_lock mapReadLock(directReadMapMutex);
272272
auto it = directReadCallbackMap.find(key);
273+
// Unlock in case callback wants to modify the map?
274+
mapReadLock.unlock();
273275
if (it != directReadCallbackMap.end()) {
274276
it->second(decoded);
275277
}

src/CAN/CANBoard.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,30 @@ CANBoard::CANBoard(robot::types::boardid_t board_id, CANDevice_t device)
1717
);
1818
sendCANPacket(p);
1919

20-
// Ping motor for configs (max vel)
20+
// Ping motor for configs
2121
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.controller.config.vel_limit"); endpoint != nullptr) {
2222
uint16_t endpoint_id = endpoint["id"];
2323
addDirectReadCallback(this->device, endpoint_id, [this, endpoint_id](auto p) {
2424
this->vel_limit = p.value_float;
25-
LOG_F(INFO, "got vel limit %f", p.value_float);
2625

2726
// We only need this once, remove after we get a response
2827
removeDirectReadCallback(this->device, endpoint_id);
2928
});
3029

3130
this->read(endpoint_id);
32-
} else {
33-
LOG_F(ERROR, "CAN Endpoints does not contain axis0.controller.config.vel_limit!");
3431
}
35-
// Has watchdog?
32+
33+
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.config.enable_watchdog"); endpoint != nullptr) {
34+
uint16_t endpoint_id = endpoint["id"];
35+
addDirectReadCallback(this->device, endpoint_id, [this, endpoint_id](auto p) {
36+
this->watchdog = p.value_bool;
37+
38+
// We only need this once, remove after we get a response
39+
removeDirectReadCallback(this->device, endpoint_id);
40+
});
41+
42+
this->read(endpoint_id);
43+
}
3644

3745
// Inversion
3846
if (auto it = robot::boardInversionMap.find(board_id); it != robot::boardInversionMap.end()) {
@@ -66,7 +74,7 @@ void CANBoard::setMotorPower(double power) {
6674
}
6775
} else {
6876
// Mapping power to a target velocity
69-
int8_t input_vel = static_cast<int8_t>(power * this->vel_limit);
77+
float input_vel = static_cast<float>(power * this->vel_limit);
7078
input_vel *= this->inversion_factor;
7179
// Make CANPacket_t
7280
CANPacket_t p = CANMotorPacket_BLDC_SetInputVelocity(
@@ -75,6 +83,20 @@ void CANBoard::setMotorPower(double power) {
7583

7684
// Send packet
7785
sendCANPacket(p);
86+
87+
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.controller.input_vel"); endpoint != nullptr) {
88+
uint16_t endpoint_id = endpoint["id"];
89+
addDirectReadCallback(this->device, endpoint_id, [input_vel](auto decoded) {
90+
if (decoded.value_float != input_vel) {
91+
LOG_F(ERROR, "Expected %f, got %f", input_vel, decoded.value_float);
92+
// send
93+
} else {
94+
// remove from map
95+
}
96+
});
97+
98+
this->read(endpoint_id);
99+
}
78100
}
79101
}
80102

src/CAN/CANBoard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ class CANBoard {
2323
private:
2424
robot::types::boardid_t board_id;
2525
CANDevice_t device;
26-
uint8_t vel_limit;
2726
int8_t inversion_factor;
27+
28+
// Configs read on startup
29+
uint8_t vel_limit;
30+
bool watchdog;
2831
};
2932

3033
} // namespace can

src/CAN/FakeCANBoard.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ int main() {
7777

7878
if (nlohmann::json endpoint = can::getEndpoint(board->get_boardid(), input); endpoint != nullptr) {
7979
uint16_t endpoint_id = endpoint["id"];
80+
// std::cout << "endpoint " << input << " has id=" << endpoint_id << std::endl;
8081
can::addDirectReadCallback(board->get_device(), endpoint_id, [board, input, endpoint](auto decoded) {
8182
std::stringstream rs("");
8283
rs << input << " from 0x" << std::hex << board->get_device().deviceUUID << ": ";
@@ -93,7 +94,7 @@ int main() {
9394
} else if (type == "float") {
9495
rs << decoded.value_float;
9596
} else if (type == "bool") {
96-
rs << decoded.value_bool;
97+
rs << (decoded.value_bool ? "true" : "false");
9798
}
9899

99100
std::cout << rs.str().c_str() << std::endl;
@@ -102,6 +103,7 @@ int main() {
102103
});
103104

104105
board->read(endpoint_id);
106+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
105107
} else {
106108
std::cout << "Unknown endpoint" << std::endl;
107109
continue;

0 commit comments

Comments
 (0)