Skip to content

Commit 72ea7fe

Browse files
committed
Fix problems
1 parent c19e6ba commit 72ea7fe

7 files changed

Lines changed: 25 additions & 62 deletions

File tree

src/CAN/CAN.cpp

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,6 @@ void handleHeartbeatPacket(CANPacket_t& packet) {
168168
}
169169
}
170170

171-
void handleLimitSwitchAlert(CANPacket_t& packet) {
172-
auto decoded = CANMotorPacket_LimitSwitchAlert_Decode(&packet);
173-
CANDeviceUUID_t uuid = packet.senderUUID;
174-
telemetrycode_t telemCode = static_cast<telemetrycode_t>(telemtype_t::limit_switch);
175-
storeTelemetry(uuid, telemCode,
176-
robot::types::DataPoint<telemetry_t>(static_cast<telemetry_t>(decoded.switchStatus)));
177-
}
178-
179171
// returns a file descriptor, or -1 on failure
180172
int createCANSocket(std::optional<CANDevice_t> device) {
181173
int fd;
@@ -227,6 +219,15 @@ int createCANSocket(std::optional<CANDevice_t> device) {
227219
return fd;
228220
}
229221

222+
bool sendCANFrame(const canfd_frame& frame) {
223+
std::lock_guard lock(socketMutex);
224+
// note that frame is a canfd_frame but we're using sizeof(can_frame)
225+
// not sure why this is required to work
226+
bool success = write(can_fd, &frame, sizeof(struct can_frame)) == sizeof(struct can_frame);
227+
tcdrain(can_fd);
228+
return success;
229+
}
230+
230231
void receiveThreadFn() {
231232
loguru::set_thread_name("CAN_Receive");
232233
CANPacket_t packet;
@@ -256,7 +257,7 @@ void receiveThreadFn() {
256257
break;
257258

258259
case CAN_COMMAND_ID__LIMIT_SWITCH_ALERT:
259-
handleLimitSwitchAlert(packet);
260+
// handleLimitSwitchAlert(packet);
260261
break;
261262

262263
case CAN_COMMAND_ID__BLDC_DIRECT_READ_RESULT:
@@ -336,15 +337,6 @@ void sendCANPacket(const CANPacket_t& packet) {
336337
}
337338
}
338339

339-
bool sendCANFrame(const canfd_frame& frame) {
340-
std::lock_guard lock(socketMutex);
341-
// note that frame is a canfd_frame but we're using sizeof(can_frame)
342-
// not sure why this is required to work
343-
bool success = write(can_fd, &frame, sizeof(struct can_frame)) == sizeof(struct can_frame);
344-
tcdrain(can_fd);
345-
return success;
346-
}
347-
348340
void printCANPacket(const CANPacket_t& packet) {
349341
CANPacket_t mutablePacket = packet; // same as sendCANPacket
350342
std::stringstream ss;
@@ -362,28 +354,6 @@ void printCANPacket(const CANPacket_t& packet) {
362354
LOG_F(INFO, ss.str().c_str());
363355
}
364356

365-
robot::types::DataPoint<telemetry_t> getDeviceTelemetry(CANDeviceUUID_t uuid,
366-
telemtype_t telemType) {
367-
std::shared_lock mapLock(telemMapMutex); // acquire read lock
368-
// find entry for device in map
369-
auto entry = telemMap.find(uuid);
370-
if (entry != telemMap.end()) {
371-
auto& devMutex = *entry->second.first;
372-
auto& devMap = *entry->second.second;
373-
// acquire read lock of device map
374-
std::shared_lock deviceLock(devMutex);
375-
// find entry for telemetry
376-
auto telemEntry = devMap.find(static_cast<telemetrycode_t>(telemType));
377-
if (telemEntry != devMap.end()) {
378-
return telemEntry->second;
379-
} else {
380-
return {};
381-
}
382-
} else {
383-
return {};
384-
}
385-
}
386-
387357
void addDirectReadCallback(CANDevice_t device, uint16_t endpoint, const std::function<void(CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>& callback) {
388358
auto key = std::make_pair(static_cast<uint8_t>(device.deviceUUID), endpoint);
389359
// Write access

src/CAN/CAN.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,6 @@ void sendCANPacket(const CANPacket_t& packet);
8787
*/
8888
void printCANPacket(const CANPacket_t& packet);
8989

90-
/**
91-
* @brief Get the latest telemetry from a CAN device.
92-
*
93-
* This method does NOT query for new data, it just returns the last reported value.
94-
*
95-
* @param id The device group and serial number of the device.
96-
* @param telemType The type of telemetry to get, as dictated by the specific device specs.
97-
* @return robot::types::DataPoint<telemetry_t> The telemetry value, with the timestamp of when
98-
* it was received. If no data is available for the given telemetry type, an empty data point
99-
* is returned.
100-
*/
101-
robot::types::DataPoint<telemetry_t> getDeviceTelemetry(uuid_t uuid, telemtype_t telemType);
102-
10390
/**
10491
* @brief Add a callback to run when we receive a read result packet corresponding
10592
* to the input endpoint. Callbacks persist until they are manually removed using

src/CAN/CANBoard.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void CANBoard::setMotorPower(double power) {
6767
this->setMotorState(can::motor::axis_state_t::closed_loop_control);
6868

6969
if (power == 0.0) {
70-
if (static_cast<uint8_t>(this->board_id) < 5) { // hack for wheels + base
70+
if (this->watchdog) {
7171
this->setMotorState(can::motor::axis_state_t::idle);
7272

7373
// hang until this actually goes idle for funsies
@@ -139,9 +139,11 @@ void CANBoard::setMotorVel(int8_t velocity) {
139139
return;
140140
}
141141

142+
float rot_vel = velocity / Constants::MILLIDEGREES_PER_REV;
143+
142144
// Make CANPacket_t
143145
CANPacket_t p = CANMotorPacket_BLDC_SetInputVelocity(
144-
Constants::JETSON_DEVICE, this->device, velocity, 0.0f
146+
Constants::JETSON_DEVICE, this->device, rot_vel, 0.0f
145147
);
146148

147149
// Send packet

src/CAN/CANBoard.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#pragma once
22

3-
#include "../world_interface/data.h"
43
#include "CAN.h"
4+
#include "../world_interface/data.h"
5+
6+
#include <mutex>
7+
#include <shared_mutex>
58

69
namespace can {
710

@@ -19,13 +22,14 @@ class CANBoard {
1922

2023
robot::types::boardid_t getBoardID() const { return board_id; }
2124
CANDevice_t getDevice() const { return device; }
22-
robot::types::DataPoint<int32_t> getPosition() const {
25+
26+
robot::types::DataPoint<int32_t> getPosition() {
2327
std::shared_lock lock(board_mutex);
2428
return this->position_mdeg;
2529
}
2630

27-
void storePosition(robot::types::DataPoint<int32_t> data) const {
28-
std::lock_guard lock(board_mutex);
31+
void storePosition(const robot::types::DataPoint<int32_t> data) {
32+
std::unique_lock lock(board_mutex);
2933
this->position_mdeg = data;
3034
}
3135

@@ -39,7 +43,7 @@ class CANBoard {
3943
bool watchdog;
4044

4145
// Estimates received
42-
std::mutex board_mutex;
46+
std::shared_mutex board_mutex;
4347
robot::types::DataPoint<int32_t> position_mdeg;
4448

4549
// debug

src/CAN/CANUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct packettype_t {
104104
using deviceserial_t = uint8_t;
105105

106106
/** @brief The type of telemetry data. */
107-
using telemetry_t = int32_t;
107+
// using telemetry_t = int32_t;
108108

109109
/**
110110
* @brief A unique identifier for a CAN device.

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ if(WORLD_INTERFACE STREQUAL "REAL")
213213
add_library(can_interface STATIC)
214214
target_sources(can_interface PRIVATE
215215
CAN/CANBoard.cpp
216-
CAN/CANMotor.cpp
217216
CAN/CANUtils.cpp
218217
)
219218

src/world_interface/real_world_interface.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ callbackid_t addLimitSwitchCallback(
288288
// auto nextID = nextCallbackID++;
289289
// callbackIDMap.insert({nextID, id});
290290
// return nextID;
291+
return 0;
291292
}
292293

293294
void removeLimitSwitchCallback(callbackid_t id) {

0 commit comments

Comments
 (0)