Skip to content

Commit

Permalink
Merge pull request REVrobotics#12 from REVrobotics/rev-hw-client
Browse files Browse the repository at this point in the history
Merge changes used in the RHC to the main branch
  • Loading branch information
Noah Andrews authored Apr 25, 2023
2 parents 2f3e8d0 + 9f96975 commit 4b8c216
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 52 deletions.
9 changes: 8 additions & 1 deletion CANBridge.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@
"fstream": "cpp",
"sstream": "cpp",
"iomanip": "cpp",
"candle_defs.h": "c"
"candle_defs.h": "c",
"compare": "cpp",
"concepts": "cpp",
"functional": "cpp",
"list": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"xhash": "cpp"
},
"java.configuration.updateBuildConfiguration": "disabled"
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ plugins {
id 'cpp'
id 'java'
id 'edu.wpi.first.wpilib.repositories.WPILibRepositoriesPlugin' version '2020.2'
id 'edu.wpi.first.NativeUtils' version '2020.6.1'
id 'edu.wpi.first.NativeUtils' version '2020.7.2'
id 'edu.wpi.first.GradleJni' version '0.10.1'
id 'edu.wpi.first.GradleVsCode' version '0.10.0'
id 'edu.wpi.first.GradleVsCode' version '0.12.0'
id 'google-test-test-suite'
}

Expand Down
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nativeUtils {
wpi {
configureDependencies {
wpiVersion = "2020.+"
niLibVersion = "2020.7.1"
niLibVersion = "2020.9.1"
opencvVersion = "3.4.7-2"
googleTestVersion = "1.9.0-3-437e100"
}
Expand Down
2 changes: 1 addition & 1 deletion publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'maven-publish'

ext.licenseFile = files("$rootDir/LICENSE.txt")

def pubVersion = '1.2.16'
def pubVersion = '2.0.1'

def outputsFolder = file("$buildDir/allOutputs")

Expand Down
2 changes: 1 addition & 1 deletion src/main/native/cpp/CANBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

#include "rev/Drivers/SerialPort/SerialDriver.h"

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

#ifdef __FRC_ROBORIO__
Expand Down
29 changes: 20 additions & 9 deletions src/main/native/cpp/Drivers/CandleWinUSB/CandleWinUSBDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <iostream> //TODO: Remove
#include <thread>

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

#define CANDLE_DEFAULT_CHANNEL 0
Expand Down Expand Up @@ -87,6 +87,10 @@ CandleWinUSBDevice::~CandleWinUSBDevice()
close_candle_dev(m_handle);
}

void CandleWinUSBDevice::ClearSendQueue() {
m_thread.clearQueue();
}

std::string CandleWinUSBDevice::GetName() const
{
return m_name;
Expand All @@ -108,6 +112,14 @@ int CandleWinUSBDevice::GetNumberOfErrors()
return m_thread.GetNumberOfErrors();
}

void CandleWinUSBDevice::setThreadPriority(utils::ThreadPriority priority) {
m_thread.setPriority(priority);
}

void CandleWinUSBDevice::stopRepeatedMessage(uint32_t messageId) {
m_thread.stopRepeatedMessage(messageId);
}

CANStatus CandleWinUSBDevice::SendCANMessage(const CANMessage& msg, int periodMs)
{
m_thread.EnqueueMessage(msg, periodMs);
Expand Down Expand Up @@ -158,15 +170,14 @@ CANStatus CandleWinUSBDevice::ReadStreamSession(uint32_t sessionHandle, struct H
return m_thread.GetLastThreadError();
}

CANStatus CandleWinUSBDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr)
CANStatus CandleWinUSBDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr) {
return GetCANDetailStatus(percentBusUtilization, busOff, txFull,receiveErr, transmitErr, nullptr);
}

CANStatus CandleWinUSBDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime)
{
rev::usb::CANStatusDetails details;
m_thread.GetCANStatus(&details);
*busOff = details.busOffCount;
*txFull = details.txFullCount;
*receiveErr = details.receiveErrCount;
*transmitErr = details.transmitErrCount;
*percentBusUtilization = 0.0; // todo how to get this
m_thread.GetCANStatusDetails(busOff, txFull, receiveErr, transmitErr, lastErrorTime);
*percentBusUtilization = 0.0; // todo how to get this

return m_thread.GetLastThreadError();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/native/cpp/Drivers/Serial/SerialDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <iostream> //TODO: Remove
#include <thread>

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

#include "serial/serial.h"
Expand Down Expand Up @@ -139,6 +139,10 @@ CANStatus SerialDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_
return m_thread.GetLastThreadError();
}

CANStatus SerialDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime) {
return GetCANDetailStatus(percentBusUtilization, busOff, txFull, receiveErr, transmitErr);
}

bool SerialDevice::IsConnected()
{
return true;
Expand Down
5 changes: 2 additions & 3 deletions src/main/native/include/rev/CANBridge.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -37,7 +37,6 @@ extern "C" {

typedef struct CANBridge_Scan* c_CANBridge_ScanHandle;


/**
* Scan for any available REV USB compatible devices.
* The scan results include only directly connected devices
Expand All @@ -51,7 +50,7 @@ c_CANBridge_ScanHandle CANBridge_Scan();
* Returns the number of devices discovered by scan. Includes
* the device used as a bridge in the count of devices detected
*
* @param previously opened c_CANBridge_ScanHandle
* @param handle previously opened c_CANBridge_ScanHandle
*
* @return number of devices detected, including the bridge device
*/
Expand Down
7 changes: 6 additions & 1 deletion src/main/native/include/rev/CANDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
#include "rev/CANMessage.h"
#include "rev/CANStatus.h"
#include "rev/CANBridgeUtils.h"
#include "utils/ThreadUtils.h"

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

namespace rev {
Expand All @@ -58,8 +59,12 @@ class CANDevice {
virtual CANStatus ReadStreamSession(uint32_t sessionHandle, HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead) = 0;

virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr) = 0;
virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime) = 0;

virtual bool IsConnected() = 0;
virtual void setThreadPriority(utils::ThreadPriority priority) {};
virtual void stopRepeatedMessage(uint32_t messageId) {};
virtual void ClearSendQueue() {};
};

} // namespace usb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,24 @@ class CandleWinUSBDevice : public CANDevice {
CandleWinUSBDevice(candle_handle hDev);
virtual ~CandleWinUSBDevice();

virtual std::string GetName() const;
virtual std::string GetDescriptor() const;
virtual int GetNumberOfErrors();
virtual std::string GetName() const override;
virtual std::string GetDescriptor() const override;
virtual int GetNumberOfErrors() override;

virtual int GetId() const;
virtual int GetId() const override;

virtual CANStatus SendCANMessage(const CANMessage& msg, int periodMs) override;
virtual CANStatus ReceiveCANMessage(std::shared_ptr<CANMessage>& msg, uint32_t messageID, uint32_t messageMask) override;
virtual CANStatus OpenStreamSession(uint32_t* sessionHandle, CANBridge_CANFilter filter, uint32_t maxSize) override;
virtual CANStatus CloseStreamSession(uint32_t sessionHandle);
virtual CANStatus ReadStreamSession(uint32_t sessionHandle, HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead);
virtual CANStatus CloseStreamSession(uint32_t sessionHandle) override;
virtual CANStatus ReadStreamSession(uint32_t sessionHandle, HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead) override;

virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr);

virtual bool IsConnected();
virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr) override;
virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime) override;
virtual bool IsConnected() override;
virtual void setThreadPriority(utils::ThreadPriority priority);
virtual void stopRepeatedMessage(uint32_t messageId);
virtual void ClearSendQueue();
private:
candle_handle m_handle;
CandleWinUSBDeviceThread m_thread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

#include "candlelib/candle.h"

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

namespace rev {
Expand Down Expand Up @@ -78,6 +78,23 @@ class CandleWinUSBDeviceThread :public DriverDeviceThread {
utils::SetThreadPriority(m_thread.get(), utils::ThreadPriority::High);
}

void setPriority(utils::ThreadPriority priority) {
if (m_thread.get() != nullptr)
utils::SetThreadPriority(m_thread.get(), priority);
}

void clearQueue() {
bool empty = m_sendQueue.empty();
}

void stopRepeatedMessage(uint32_t messageId) {
for (int i = 0; i < m_sendQueue.size(); i++) {
detail::CANThreadSendQueueElement el = m_sendQueue.front();
m_sendQueue.pop();
if (el.m_msg.GetMessageId() != messageId) m_sendQueue.push(el);
}
}

void OpenStream(uint32_t* handle, CANBridge_CANFilter filter, uint32_t maxSize, CANStatus *status) override {
std::lock_guard<std::mutex> lock(m_streamMutex);

Expand All @@ -99,11 +116,11 @@ class CandleWinUSBDeviceThread :public DriverDeviceThread {
candle_frame_t incomingFrame;

reading = candle_frame_read(m_device, &incomingFrame, 0);

// Received a new frame, store it
if (reading) {
candle_frametype_t frameType = candle_frame_type(&incomingFrame);
if(frameType == CANDLE_FRAMETYPE_ERROR) {
time(&lastErrorTime);
// Parse error data
if (incomingFrame.can_id & 0x00000040) {
m_statusDetails.busOffCount++;
Expand Down Expand Up @@ -149,7 +166,7 @@ class CandleWinUSBDeviceThread :public DriverDeviceThread {
}

bool WriteMessages(detail::CANThreadSendQueueElement el, std::chrono::steady_clock::time_point now) {
if (el.m_intervalMs == 0 || (now - el.m_prevTimestamp >= std::chrono::milliseconds(el.m_intervalMs)) ) {
if (el.m_intervalMs <= 1 || (now - el.m_prevTimestamp >= std::chrono::milliseconds(el.m_intervalMs)) ) {
candle_frame_t frame;
frame.can_dlc = el.m_msg.GetSize();
// set extended id flag
Expand Down Expand Up @@ -186,21 +203,24 @@ class CandleWinUSBDeviceThread :public DriverDeviceThread {
if (m_sendQueue.size() > 0) {
detail::CANThreadSendQueueElement el = m_sendQueue.front();
if (el.m_intervalMs == -1) {
m_sendQueue.pop();
while(m_sendQueue.size() > 0) {
m_sendQueue.pop();
}
continue;
}

auto now = std::chrono::steady_clock::now();

// Don't pop queue if send fails
m_sendQueue.pop();
if (WriteMessages(el, now)) {
m_sendQueue.pop();

// Return to end of queue if repeated
if (el.m_intervalMs > 0 ) {
el.m_prevTimestamp = now;
m_sendQueue.push(el);
}
} else {
m_sendQueue.push(el);
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/native/include/rev/Drivers/DriverDeviceThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <map>
#include <queue>
#include <memory>
#include <time.h>

#include <iostream>

Expand All @@ -46,7 +47,7 @@

#include "utils/ThreadUtils.h"

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

namespace rev {
Expand All @@ -64,7 +65,7 @@ class DriverDeviceThread {
m_threadIntervalMs(threadIntervalMs),
m_counter(counter) {
}

virtual void Start() = 0;

void Stop() {
Expand Down Expand Up @@ -133,6 +134,14 @@ class DriverDeviceThread {
details = &m_statusDetails;
}

void GetCANStatusDetails(uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* errorTime) {
*busOff = m_statusDetails.busOffCount;
*txFull = m_statusDetails.txFullCount;
*receiveErr = m_statusDetails.receiveErrCount;
*transmitErr = m_statusDetails.transmitErrCount;
*errorTime = lastErrorTime;
}

CANStatus GetLastThreadError() {
CANStatus last = m_threadStatus;
m_threadStatus = CANStatus::kOk;
Expand All @@ -156,6 +165,7 @@ class DriverDeviceThread {
uint32_t m_counter;

CANStatusDetails m_statusDetails;
time_t lastErrorTime;
int m_statusErrCount = 0;
CANStatus m_threadStatus = CANStatus::kOk;

Expand Down
17 changes: 9 additions & 8 deletions src/main/native/include/rev/Drivers/SerialPort/SerialDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ class SerialDevice : public CANDevice {
SerialDevice(std::string port);
virtual ~SerialDevice();

virtual std::string GetName() const;
virtual std::string GetDescriptor() const;
virtual int GetNumberOfErrors();
virtual std::string GetName() const override;
virtual std::string GetDescriptor() const override;
virtual int GetNumberOfErrors() override;

virtual int GetId() const;
virtual int GetId() const override;

virtual CANStatus SendCANMessage(const CANMessage& msg, int periodMs) override;
virtual CANStatus ReceiveCANMessage(std::shared_ptr<CANMessage>& msg, uint32_t messageID, uint32_t messageMask) override;
virtual CANStatus OpenStreamSession(uint32_t* sessionHandle, CANBridge_CANFilter filter, uint32_t maxSize) override;
virtual CANStatus CloseStreamSession(uint32_t sessionHandle);
virtual CANStatus ReadStreamSession(uint32_t sessionHandle, HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead);
virtual CANStatus CloseStreamSession(uint32_t sessionHandle) override;
virtual CANStatus ReadStreamSession(uint32_t sessionHandle, HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead) override;

virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr);
virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr) override;
virtual CANStatus GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime) override;

virtual bool IsConnected();
virtual bool IsConnected() override;
private:
SerialDeviceThread m_thread;
std::string m_descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

#include "serial/serial.h"

#include <mockdata/CanData.h>
#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

namespace rev {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SerialDriver : public CANDriver {
SerialDriver() {}
virtual ~SerialDriver() override {}

virtual std::string GetName() const {return "Legacy";}
virtual std::string GetName() const override {return "Legacy";}

virtual std::vector<CANDeviceDetail> GetDevices() override;
virtual std::unique_ptr<CANDevice> CreateDeviceFromDescriptor(const char* descriptor) override;
Expand Down
Loading

0 comments on commit 4b8c216

Please sign in to comment.