2024-11-30 18:04:00 +00:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/DutyCycle.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <thread>
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "HALInitializer.hpp"
|
|
|
|
|
#include "PortsInternal.hpp"
|
|
|
|
|
#include "SmartIo.hpp"
|
2026-04-17 20:19:38 -07:00
|
|
|
#include "wpi/hal/ErrorHandling.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Errors.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/handles/HandlesInternal.hpp"
|
2026-03-15 15:08:41 -07:00
|
|
|
#include "wpi/hal/monotonic_clock.hpp"
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::hal;
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::init {
|
2024-11-30 18:04:00 +00:00
|
|
|
void InitializeDutyCycle() {}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
extern "C" {
|
2025-01-30 18:59:34 -08:00
|
|
|
HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
|
2025-01-15 11:57:31 -08:00
|
|
|
const char* allocationLocation,
|
2024-11-30 18:04:00 +00:00
|
|
|
int32_t* status) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::init::CheckInit();
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
if (channel < 0 || channel >= kNumSmartIo) {
|
2026-04-19 16:12:18 -07:00
|
|
|
*status = MakeErrorIndexOutOfRange(HAL_RESOURCE_OUT_OF_RANGE,
|
2026-04-17 20:19:38 -07:00
|
|
|
"Invalid Index for DutyCycle", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2025-01-15 11:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
auto resource = smartIoHandles->Allocate(channel, HAL_HandleEnum::DUTY_CYCLE,
|
|
|
|
|
"DutyCycle");
|
2025-01-15 11:57:31 -08:00
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
if (!resource) {
|
|
|
|
|
*status = resource.error();
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE; // failed to allocate. Pass error back.
|
2025-01-15 11:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
auto [handle, port] = *resource;
|
2025-01-15 11:57:31 -08:00
|
|
|
port->channel = channel;
|
|
|
|
|
|
2026-06-06 14:54:49 -07:00
|
|
|
*status = port->InitializeMode(MRC_SmartIOMode::MRC_SmartIOMode_PwmInput);
|
2025-01-15 11:57:31 -08:00
|
|
|
if (*status != 0) {
|
2026-03-16 21:49:21 -07:00
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::DUTY_CYCLE);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2025-01-15 11:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DUTY_CYCLE);
|
2025-01-15 11:57:31 -08:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 21:49:21 -07:00
|
|
|
smartIoHandles->Free(dutyCycleHandle, HAL_HandleEnum::DUTY_CYCLE);
|
2025-01-15 11:57:31 -08:00
|
|
|
|
|
|
|
|
// Wait for no other object to hold this handle.
|
2026-03-15 15:08:41 -07:00
|
|
|
auto start = wpi::hal::monotonic_clock::now();
|
2025-01-15 11:57:31 -08:00
|
|
|
while (port.use_count() != 1) {
|
2026-03-15 15:08:41 -07:00
|
|
|
auto current = wpi::hal::monotonic_clock::now();
|
2025-01-15 11:57:31 -08:00
|
|
|
if (start + std::chrono::seconds(1) < current) {
|
2026-06-06 14:54:49 -07:00
|
|
|
std::puts("DutyCycle handle free timeout");
|
2025-01-15 11:57:31 -08:00
|
|
|
std::fflush(stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 14:54:49 -07:00
|
|
|
void HAL_SetDutyCycleSimDevice(HAL_DutyCycleHandle handle,
|
2024-11-30 18:04:00 +00:00
|
|
|
HAL_SimDeviceHandle device) {}
|
|
|
|
|
|
2025-07-14 23:46:17 -07:00
|
|
|
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DUTY_CYCLE);
|
2025-07-14 23:46:17 -07:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t ret = 0;
|
|
|
|
|
*status = port->GetPwmInputPeriodMicroseconds(&ret);
|
|
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1000000.0 / ret;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetDutyCycleOutput(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DUTY_CYCLE);
|
2025-07-14 23:46:17 -07:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t highTime = 0;
|
|
|
|
|
*status = port->GetPwmInputMicroseconds(&highTime);
|
|
|
|
|
|
|
|
|
|
uint16_t period = 0;
|
|
|
|
|
*status = port->GetPwmInputPeriodMicroseconds(&period);
|
|
|
|
|
|
|
|
|
|
if (period == 0) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (highTime >= period) {
|
|
|
|
|
return 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static_cast<double>(highTime) / static_cast<double>(period);
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DUTY_CYCLE);
|
2025-01-15 11:57:31 -08:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
2025-07-14 23:46:17 -07:00
|
|
|
return 0;
|
2025-01-15 11:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
2025-06-01 22:24:48 -07:00
|
|
|
uint16_t ret = 0;
|
2025-01-15 11:57:31 -08:00
|
|
|
*status = port->GetPwmInputMicroseconds(&ret);
|
2025-06-01 22:24:48 -07:00
|
|
|
return static_cast<int32_t>(ret) * 1000;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
} // extern "C"
|