blob: 85646cfab96eb1e9bf27eafa9cef9f0d368ed2f8 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include "hal/PowerDistribution.h"
6
7#include <fmt/format.h>
8
9#include "CANAPIInternal.h"
10#include "HALInitializer.h"
11#include "HALInternal.h"
12#include "PortsInternal.h"
13#include "hal/CANAPI.h"
14#include "hal/Errors.h"
15#include "mockdata/PowerDistributionDataInternal.h"
16
17using namespace hal;
18
19static constexpr HAL_CANManufacturer manufacturer =
20 HAL_CANManufacturer::HAL_CAN_Man_kCTRE;
21
22static constexpr HAL_CANDeviceType deviceType =
23 HAL_CANDeviceType::HAL_CAN_Dev_kPowerDistribution;
24
25namespace hal::init {
26void InitializePowerDistribution() {}
27} // namespace hal::init
28
29extern "C" {
30HAL_PowerDistributionHandle HAL_InitializePowerDistribution(
31 int32_t module, HAL_PowerDistributionType type,
32 const char* allocationLocation, int32_t* status) {
33 if (!HAL_CheckPowerDistributionModule(module, type)) {
34 *status = PARAMETER_OUT_OF_RANGE;
35 hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
36 return HAL_kInvalidHandle;
37 }
38 hal::init::CheckInit();
39 SimPowerDistributionData[module].initialized = true;
40 auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
41
42 if (*status != 0) {
43 HAL_CleanCAN(handle);
44 return HAL_kInvalidHandle;
45 }
46
47 return handle;
48}
49
50int32_t HAL_GetPowerDistributionModuleNumber(HAL_PowerDistributionHandle handle,
51 int32_t* status) {
52 auto module = hal::can::GetCANModuleFromHandle(handle, status);
53 if (*status != 0) {
54 return 0;
55 }
56 return module;
57}
58
59HAL_Bool HAL_CheckPowerDistributionModule(int32_t module,
60 HAL_PowerDistributionType type) {
61 if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
62 return module < kNumCTREPDPModules && module >= 0;
63 } else {
64 return module < kNumREVPDHModules && module >= 1;
65 }
66}
67
68HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle,
69 int32_t channel) {
70 // TODO make this grab from the handle directly
71 if (false) {
72 return channel < kNumCTREPDPChannels && channel >= 0;
73 } else {
74 return channel < kNumREVPDHChannels && channel >= 0;
75 }
76}
77
78HAL_PowerDistributionType HAL_GetPowerDistributionType(
79 HAL_PowerDistributionHandle handle, int32_t* status) {
80 return HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE;
81}
82
83int32_t HAL_GetPowerDistributionNumChannels(HAL_PowerDistributionHandle handle,
84 int32_t* status) {
85 // TODO make this grab from the handle directly
86 if (false) {
87 return kNumCTREPDPChannels;
88 } else {
89 return kNumREVPDHChannels;
90 }
91}
92
93void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) {
94 HAL_CleanCAN(handle);
95}
96
97double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle,
98 int32_t* status) {
99 auto module = hal::can::GetCANModuleFromHandle(handle, status);
100 if (*status != 0) {
101 return 0.0;
102 }
103 return SimPowerDistributionData[module].temperature;
104}
105double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle,
106 int32_t* status) {
107 auto module = hal::can::GetCANModuleFromHandle(handle, status);
108 if (*status != 0) {
109 return 0.0;
110 }
111 return SimPowerDistributionData[module].voltage;
112}
113double HAL_GetPowerDistributionChannelCurrent(
114 HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) {
115 auto module = hal::can::GetCANModuleFromHandle(handle, status);
116 if (*status != 0) {
117 return 0.0;
118 }
119 return SimPowerDistributionData[module].current[channel];
120}
121void HAL_GetPowerDistributionAllChannelCurrents(
122 HAL_PowerDistributionHandle handle, double* currents,
123 int32_t currentsLength, int32_t* status) {
124 auto module = hal::can::GetCANModuleFromHandle(handle, status);
125 if (*status != 0) {
126 return;
127 }
128
129 auto& data = SimPowerDistributionData[module];
130 int toCopy = (std::min)(currentsLength, kNumPDSimChannels);
131 for (int i = 0; i < toCopy; i++) {
132 currents[i] = data.current[i];
133 }
134}
135double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle,
136 int32_t* status) {
137 return 0.0;
138}
139double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle,
140 int32_t* status) {
141 return 0.0;
142}
143double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
144 int32_t* status) {
145 return 0.0;
146}
147void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
148 int32_t* status) {}
149void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle,
150 int32_t* status) {}
151void HAL_SetPowerDistributionSwitchableChannel(
152 HAL_PowerDistributionHandle handle, HAL_Bool state, int32_t* status) {}
153
154HAL_Bool HAL_GetPowerDistributionSwitchableChannel(
155 HAL_PowerDistributionHandle handle, int32_t* status) {
156 return false;
157}
158} // extern "C"