Skip to content

Commit 157faf2

Browse files
committed
Fix brake behavior
1 parent 68c4085 commit 157faf2

4 files changed

Lines changed: 41 additions & 35 deletions

File tree

src/CAN/CAN.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ bool receivePacket(int fd, CANPacket_t& packet) {
117117

118118
void handleAck(CANPacket_t& packet) {
119119
auto decoded = CANUniversalPacket_Acknowledge_Decode(&packet);
120+
if (decoded.sender.deviceUUID == 16) {
121+
// 0x10
122+
return;
123+
}
120124
if (decoded.failure) {
121125
LOG_F(WARNING, "Ack received from 0x%x: FAIL", decoded.sender.deviceUUID);
122126
} else {

src/CAN/CANBoard.cpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,35 @@ void CANBoard::setMotorPower(double power) {
6565
return;
6666
}
6767

68-
// Fetch motor states
69-
70-
// Ensure motor control mode is velocity
71-
// Ensure motor state is closed loop control
72-
this->setMotorState(can::motor::axis_state_t::closed_loop_control);
73-
7468
if (power == 0.0) {
7569
if (this->board_id == robot::types::boardid_t::shoulder || this->board_id == robot::types::boardid_t::elbow) {
7670
// Set brake
7771
this->setBrake(BRAKE_ON);
7872
}
7973

80-
if (this->watchdog) {
81-
this->setMotorState(can::motor::axis_state_t::idle);
82-
83-
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.current_state"); endpoint != nullptr) {
84-
uint16_t endpoint_id = endpoint["id"];
85-
addDirectReadCallback(this->device, endpoint_id, [this, endpoint_id](auto decoded) {
86-
if (decoded.value_uint8 != static_cast<uint8_t>(can::motor::axis_state_t::idle)) {
87-
LOG_F(ERROR, "0x%x (%s) DID NOT LISTEN AND IS NOT IDLE AND IS INSTEAD %d, re-attempting...", this->device.deviceUUID, util::to_string(this->board_id).c_str(), decoded.value_uint8);
88-
this->setMotorState(can::motor::axis_state_t::idle);
89-
this->read(endpoint_id);
90-
} else {
91-
removeDirectReadCallback(this->device, endpoint_id);
92-
}
93-
});
94-
95-
this->read(endpoint_id);
96-
}
97-
}
74+
this->setMotorState(can::motor::axis_state_t::idle);
75+
76+
// if (this->watchdog) {
77+
// if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.current_state"); endpoint != nullptr) {
78+
// uint16_t endpoint_id = endpoint["id"];
79+
// addDirectReadCallback(this->device, endpoint_id, [this, endpoint_id](auto decoded) {
80+
// if (decoded.value_uint8 != static_cast<uint8_t>(can::motor::axis_state_t::idle)) {
81+
// LOG_F(ERROR, "0x%x (%s) DID NOT LISTEN AND IS NOT IDLE AND IS INSTEAD %d, re-attempting...", this->device.deviceUUID, util::to_string(this->board_id).c_str(), decoded.value_uint8);
82+
// this->setMotorState(can::motor::axis_state_t::idle);
83+
// this->read(endpoint_id);
84+
// } else {
85+
// removeDirectReadCallback(this->device, endpoint_id);
86+
// }
87+
// });
88+
89+
// this->read(endpoint_id);
90+
// }
91+
// }
9892
} else {
93+
// Ensure motor state is closed loop control
94+
this->setMotorState(can::motor::axis_state_t::closed_loop_control);
9995
// Mapping power to a target velocity
100-
float input_vel = static_cast<float>(power * this->vel_limit) * 0.4; // hard-coded 40%
101-
input_vel *= this->inversion_factor;
102-
96+
this->input_vel = static_cast<float>(power * this->vel_limit) * 0.4 * this->inversion_factor; // hard-coded 40%
10397
// LOG_F(INFO, "True input velocity %f, ", input_vel);
10498

10599
if (this->board_id == robot::types::boardid_t::shoulder || this->board_id == robot::types::boardid_t::elbow) {
@@ -116,12 +110,12 @@ void CANBoard::setMotorPower(double power) {
116110

117111
if (nlohmann::json endpoint = getEndpoint(this->board_id, "axis0.controller.input_vel"); endpoint != nullptr) {
118112
uint16_t endpoint_id = endpoint["id"];
119-
addDirectReadCallback(this->device, endpoint_id, [input_vel, p, this, endpoint_id](auto decoded) {
113+
addDirectReadCallback(this->device, endpoint_id, [p, this, endpoint_id](auto decoded) {
120114
if (decoded.value_float != input_vel) {
121-
LOG_F(ERROR, "Expected %f, got %f", input_vel, decoded.value_float);
122-
sendCANPacket(p);
115+
LOG_F(ERROR, "Expected %f, got %f", this->input_vel, decoded.value_float);
116+
// sendCANPacket(p);
123117
} else {
124-
LOG_F(INFO, "Got %f vel_limit :)", decoded.value_float);
118+
// LOG_F(INFO, "Got %f vel_limit :)", decoded.value_float);
125119
}
126120
});
127121

src/CAN/CANBoard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class CANBoard {
4848
robot::types::boardid_t board_id;
4949
CANDevice_t device;
5050
int8_t inversion_factor;
51+
float input_vel;
5152

5253
// Configs read on startup
5354
float vel_limit;

src/CAN/FakeCANBoard.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,18 @@ int main() {
187187
can::sendCANPacket(p);
188188
} else if (testMode == TestMode::Debug) {
189189
uint8_t brakeID = prompt("brake ID");
190-
uint8_t state = prompt("state");
191-
CANPacket_t packet = CANPeripheralPacket_SetBrakes(Constants::JETSON_DEVICE, device, brakeID, state);
190+
// uint8_t state = prompt("state");
191+
192+
CANPacket_t on = CANPeripheralPacket_SetBrakes(Constants::JETSON_DEVICE, device, brakeID, 0);
193+
CANPacket_t off = CANPeripheralPacket_SetBrakes(Constants::JETSON_DEVICE, device, brakeID, 1);
194+
195+
while (true) {
196+
can::sendCANPacket(on);
197+
std::this_thread::sleep_for(std::chrono::seconds(5));
198+
can::sendCANPacket(off);
199+
std::this_thread::sleep_for(std::chrono::seconds(5));
200+
}
192201

193-
can::printCANPacket(packet);
194-
can::sendCANPacket(packet);
195202
}
196203
}
197204
}

0 commit comments

Comments
 (0)