diff --git a/hal/src/main/java/edu/wpi/first/hal/CANAPIJNI.java b/hal/src/main/java/edu/wpi/first/hal/CANAPIJNI.java index 1ac3ca7365..8d805e09e7 100644 --- a/hal/src/main/java/edu/wpi/first/hal/CANAPIJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/CANAPIJNI.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -26,7 +26,4 @@ public class CANAPIJNI extends JNIWrapper { public static native boolean readCANPacketTimeout(int handle, int apiId, int timeoutMs, CANData data); - - public static native boolean readCANPeriodicPacket(int handle, int apiId, int timeoutMs, - int periodMs, CANData data); } diff --git a/hal/src/main/native/athena/CANAPI.cpp b/hal/src/main/native/athena/CANAPI.cpp index aa41224f84..e2d351e5b2 100644 --- a/hal/src/main/native/athena/CANAPI.cpp +++ b/hal/src/main/native/athena/CANAPI.cpp @@ -68,6 +68,8 @@ static int32_t CreateCANId(CANStorage* storage, int32_t apiId) { return createdId; } +extern "C" { + HAL_CANHandle HAL_InitializeCAN(HAL_CANManufacturer manufacturer, int32_t deviceId, HAL_CANDeviceType deviceType, int32_t* status) { @@ -262,65 +264,4 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, } } } - -void HAL_ReadCANPeriodicPacket(HAL_CANHandle handle, int32_t apiId, - uint8_t* data, int32_t* length, - uint64_t* receivedTimestamp, int32_t timeoutMs, - int32_t periodMs, int32_t* status) { - auto can = canHandles->Get(handle); - if (!can) { - *status = HAL_HANDLE_ERROR; - return; - } - - uint32_t messageId = CreateCANId(can.get(), apiId); - - { - std::scoped_lock lock(can->mapMutex); - auto i = can->receives.find(messageId); - if (i != can->receives.end()) { - // Found, check if new enough - uint32_t now = GetPacketBaseTime(); - if (now - i->second.lastTimeStamp < static_cast(periodMs)) { - // Read the data from the stored message into the output - std::memcpy(data, i->second.data, i->second.length); - *length = i->second.length; - *receivedTimestamp = i->second.lastTimeStamp; - *status = 0; - return; - } - } - } - - uint8_t dataSize = 0; - uint32_t ts = 0; - HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - - std::scoped_lock lock(can->mapMutex); - if (*status == 0) { - // fresh update - auto& msg = can->receives[messageId]; - msg.length = dataSize; - *length = dataSize; - msg.lastTimeStamp = ts; - *receivedTimestamp = ts; - // The NetComm call placed in data, copy into the msg - std::memcpy(msg.data, data, dataSize); - } else { - auto i = can->receives.find(messageId); - if (i != can->receives.end()) { - // Found, check if new enough - uint32_t now = GetPacketBaseTime(); - if (now - i->second.lastTimeStamp > static_cast(timeoutMs)) { - // Timeout, return bad status - *status = HAL_CAN_TIMEOUT; - return; - } - // Read the data from the stored message into the output - std::memcpy(data, i->second.data, i->second.length); - *length = i->second.length; - *receivedTimestamp = i->second.lastTimeStamp; - *status = 0; - } - } -} +} // extern "C" diff --git a/hal/src/main/native/athena/PDP.cpp b/hal/src/main/native/athena/PDP.cpp index b9daf413df..f27f5da82d 100644 --- a/hal/src/main/native/athena/PDP.cpp +++ b/hal/src/main/native/athena/PDP.cpp @@ -34,7 +34,6 @@ static constexpr int32_t StatusEnergy = 0x5D; static constexpr int32_t Control1 = 0x70; static constexpr int32_t TimeoutMs = 100; -static constexpr int32_t StatusPeriodMs = 25; /* encoder/decoders */ union PdpStatus1 { @@ -173,9 +172,8 @@ double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) { int32_t length = 0; uint64_t receivedTimestamp = 0; - HAL_ReadCANPeriodicPacket(handle, Status3, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); return pdpStatus.bits.temp * 1.03250836957542 - 67.8564500484966; } @@ -185,9 +183,8 @@ double HAL_GetPDPVoltage(HAL_PDPHandle handle, int32_t* status) { int32_t length = 0; uint64_t receivedTimestamp = 0; - HAL_ReadCANPeriodicPacket(handle, Status3, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); return pdpStatus.bits.busVoltage * 0.05 + 4.0; /* 50mV per unit plus 4V. */ } @@ -206,9 +203,8 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel, if (channel <= 5) { PdpStatus1 pdpStatus; - HAL_ReadCANPeriodicPacket(handle, Status1, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, Status1, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); switch (channel) { case 0: raw = (static_cast(pdpStatus.bits.chan1_h8) << 2) | @@ -237,9 +233,8 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel, } } else if (channel <= 11) { PdpStatus2 pdpStatus; - HAL_ReadCANPeriodicPacket(handle, Status2, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, Status2, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); switch (channel) { case 6: raw = (static_cast(pdpStatus.bits.chan7_h8) << 2) | @@ -268,9 +263,8 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel, } } else { PdpStatus3 pdpStatus; - HAL_ReadCANPeriodicPacket(handle, Status3, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); switch (channel) { case 12: raw = (static_cast(pdpStatus.bits.chan13_h8) << 2) | @@ -300,9 +294,8 @@ double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status) { int32_t length = 0; uint64_t receivedTimestamp = 0; - HAL_ReadCANPeriodicPacket(handle, StatusEnergy, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); uint32_t raw; raw = pdpStatus.bits.TotalCurrent_125mAperunit_h8; @@ -316,9 +309,8 @@ double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status) { int32_t length = 0; uint64_t receivedTimestamp = 0; - HAL_ReadCANPeriodicPacket(handle, StatusEnergy, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); uint32_t raw; raw = pdpStatus.bits.Power_125mWperunit_h4; @@ -334,9 +326,8 @@ double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) { int32_t length = 0; uint64_t receivedTimestamp = 0; - HAL_ReadCANPeriodicPacket(handle, StatusEnergy, pdpStatus.data, &length, - &receivedTimestamp, TimeoutMs, StatusPeriodMs, - status); + HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length, + &receivedTimestamp, TimeoutMs, status); uint32_t raw; raw = pdpStatus.bits.Energy_125mWPerUnitXTmeas_h4; diff --git a/hal/src/main/native/cpp/jni/CANAPIJNI.cpp b/hal/src/main/native/cpp/jni/CANAPIJNI.cpp index 8552f9106c..eb958c5e77 100644 --- a/hal/src/main/native/cpp/jni/CANAPIJNI.cpp +++ b/hal/src/main/native/cpp/jni/CANAPIJNI.cpp @@ -203,38 +203,4 @@ Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketTimeout reinterpret_cast(dataTemp)); return true; } - -/* - * Class: edu_wpi_first_hal_CANAPIJNI - * Method: readCANPeriodicPacket - * Signature: (IIIILjava/lang/Object;)Z - */ -JNIEXPORT jboolean JNICALL -Java_edu_wpi_first_hal_CANAPIJNI_readCANPeriodicPacket - (JNIEnv* env, jclass, jint handle, jint apiId, jint timeoutMs, jint periodMs, - jobject data) -{ - auto halHandle = static_cast(handle); - uint8_t dataTemp[8]; - int32_t dataLength = 0; - uint64_t timestamp = 0; - int32_t status = 0; - HAL_ReadCANPeriodicPacket(halHandle, apiId, dataTemp, &dataLength, ×tamp, - timeoutMs, periodMs, &status); - if (status == HAL_CAN_TIMEOUT || - status == HAL_ERR_CANSessionMux_MessageNotFound) { - return false; - } - if (!CheckStatus(env, status)) { - return false; - } - if (dataLength > 8) dataLength = 8; - - jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp); - auto javaLen = env->GetArrayLength(toSetArray); - if (javaLen < dataLength) dataLength = javaLen; - env->SetByteArrayRegion(toSetArray, 0, dataLength, - reinterpret_cast(dataTemp)); - return true; -} } // extern "C" diff --git a/hal/src/main/native/include/hal/CANAPI.h b/hal/src/main/native/include/hal/CANAPI.h index 48c254dde7..2ddd6c69d6 100644 --- a/hal/src/main/native/include/hal/CANAPI.h +++ b/hal/src/main/native/include/hal/CANAPI.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -134,28 +134,6 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, uint64_t* receivedTimestamp, int32_t timeoutMs, int32_t* status); -/** - * Reads a CAN packet. The will return the last packet received until the - * packet is older then the requested timeout. Then it will return an error - * code. The period parameter is used when you know the packet is sent at - * specific intervals, so calls will not attempt to read a new packet from the - * network until that period has passed. We do not recommend users use this - * API unless they know the implications. - * - * @param handle the CAN handle - * @param apiId the ID to read (0-1023) - * @param data the packet data (8 bytes) - * @param length the received length (0-8 bytes) - * @param receivedTimestamp the packet received timestamp (based off of - * CLOCK_MONOTONIC) - * @param timeoutMs the timeout time for the packet - * @param periodMs the standard period for the packet - */ -void HAL_ReadCANPeriodicPacket(HAL_CANHandle handle, int32_t apiId, - uint8_t* data, int32_t* length, - uint64_t* receivedTimestamp, int32_t timeoutMs, - int32_t periodMs, int32_t* status); - #ifdef __cplusplus } // extern "C" #endif diff --git a/wpilibc/src/main/native/cpp/CAN.cpp b/wpilibc/src/main/native/cpp/CAN.cpp index 711645fd6d..9c81ba6f16 100644 --- a/wpilibc/src/main/native/cpp/CAN.cpp +++ b/wpilibc/src/main/native/cpp/CAN.cpp @@ -116,20 +116,3 @@ bool CAN::ReadPacketTimeout(int apiId, int timeoutMs, CANData* data) { return true; } } - -bool CAN::ReadPeriodicPacket(int apiId, int timeoutMs, int periodMs, - CANData* data) { - int32_t status = 0; - HAL_ReadCANPeriodicPacket(m_handle, apiId, data->data, &data->length, - &data->timestamp, timeoutMs, periodMs, &status); - if (status == HAL_CAN_TIMEOUT || - status == HAL_ERR_CANSessionMux_MessageNotFound) { - return false; - } - if (status != 0) { - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); - return false; - } else { - return true; - } -} diff --git a/wpilibc/src/main/native/include/frc/CAN.h b/wpilibc/src/main/native/include/frc/CAN.h index 5ef4e21147..8c383be63b 100644 --- a/wpilibc/src/main/native/include/frc/CAN.h +++ b/wpilibc/src/main/native/include/frc/CAN.h @@ -122,23 +122,6 @@ class CAN : public ErrorBase { */ bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data); - /** - * Read a CAN packet. The will return the last packet received until the - * packet is older then the requested timeout. Then it will return false. The - * period parameter is used when you know the packet is sent at specific - * intervals, so calls will not attempt to read a new packet from the network - * until that period has passed. We do not recommend users use this API unless - * they know the implications. - * - * @param apiId The API ID to read. - * @param timeoutMs The timeout time for the packet - * @param periodMs The usual period for the packet - * @param data Storage for the received data. - * @return True if the data is valid, otherwise false. - */ - bool ReadPeriodicPacket(int apiId, int timeoutMs, int periodMs, - CANData* data); - static constexpr HAL_CANManufacturer kTeamManufacturer = HAL_CAN_Man_kTeamUse; static constexpr HAL_CANDeviceType kTeamDeviceType = HAL_CAN_Dev_kMiscellaneous; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/CAN.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/CAN.java index c6eac3ac81..67d4c1cc2a 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/CAN.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/CAN.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -134,22 +134,4 @@ public class CAN implements Closeable { public boolean readPacketTimeout(int apiId, int timeoutMs, CANData data) { return CANAPIJNI.readCANPacketTimeout(m_handle, apiId, timeoutMs, data); } - - /** - * Read a CAN packet. The will return the last packet received until the - * packet is older then the requested timeout. Then it will return false. - * The period parameter is used when you know the packet is sent at specific - * intervals, so calls will not attempt to read a new packet from the - * network until that period has passed. We do not recommend users use this - * API unless they know the implications. - * - * @param apiId The API ID to read. - * @param timeoutMs The timeout time for the packet - * @param periodMs The usual period for the packet - * @param data Storage for the received data. - * @return True if the data is valid, otherwise false. - */ - public boolean readPeriodicPacket(int apiId, int timeoutMs, int periodMs, CANData data) { - return CANAPIJNI.readCANPeriodicPacket(m_handle, apiId, timeoutMs, periodMs, data); - } }