blob: 22e4bba258da09046dc7361aeacb8c7349758ca7 [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) {
Austin Schuh75263e32022-02-22 18:05:32 -080033 if (type == HAL_PowerDistributionType_kAutomatic) {
34 if (module != HAL_DEFAULT_POWER_DISTRIBUTION_MODULE) {
35 *status = PARAMETER_OUT_OF_RANGE;
36 hal::SetLastError(
37 status, "Automatic PowerDistributionType must have default module");
38 return HAL_kInvalidHandle;
39 }
40
41 // TODO Make this not matter
42 type = HAL_PowerDistributionType_kCTRE;
43 module = 0;
44 }
45
Austin Schuh812d0d12021-11-04 20:16:48 -070046 if (!HAL_CheckPowerDistributionModule(module, type)) {
47 *status = PARAMETER_OUT_OF_RANGE;
48 hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
49 return HAL_kInvalidHandle;
50 }
51 hal::init::CheckInit();
52 SimPowerDistributionData[module].initialized = true;
53 auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
54
55 if (*status != 0) {
56 HAL_CleanCAN(handle);
57 return HAL_kInvalidHandle;
58 }
59
60 return handle;
61}
62
63int32_t HAL_GetPowerDistributionModuleNumber(HAL_PowerDistributionHandle handle,
64 int32_t* status) {
65 auto module = hal::can::GetCANModuleFromHandle(handle, status);
66 if (*status != 0) {
67 return 0;
68 }
69 return module;
70}
71
72HAL_Bool HAL_CheckPowerDistributionModule(int32_t module,
73 HAL_PowerDistributionType type) {
74 if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
75 return module < kNumCTREPDPModules && module >= 0;
76 } else {
77 return module < kNumREVPDHModules && module >= 1;
78 }
79}
80
81HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle,
82 int32_t channel) {
83 // TODO make this grab from the handle directly
84 if (false) {
85 return channel < kNumCTREPDPChannels && channel >= 0;
86 } else {
87 return channel < kNumREVPDHChannels && channel >= 0;
88 }
89}
90
91HAL_PowerDistributionType HAL_GetPowerDistributionType(
92 HAL_PowerDistributionHandle handle, int32_t* status) {
93 return HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE;
94}
95
96int32_t HAL_GetPowerDistributionNumChannels(HAL_PowerDistributionHandle handle,
97 int32_t* status) {
98 // TODO make this grab from the handle directly
99 if (false) {
100 return kNumCTREPDPChannels;
101 } else {
102 return kNumREVPDHChannels;
103 }
104}
105
106void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) {
107 HAL_CleanCAN(handle);
108}
109
110double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle,
111 int32_t* status) {
112 auto module = hal::can::GetCANModuleFromHandle(handle, status);
113 if (*status != 0) {
114 return 0.0;
115 }
116 return SimPowerDistributionData[module].temperature;
117}
118double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle,
119 int32_t* status) {
120 auto module = hal::can::GetCANModuleFromHandle(handle, status);
121 if (*status != 0) {
122 return 0.0;
123 }
124 return SimPowerDistributionData[module].voltage;
125}
126double HAL_GetPowerDistributionChannelCurrent(
127 HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) {
128 auto module = hal::can::GetCANModuleFromHandle(handle, status);
129 if (*status != 0) {
130 return 0.0;
131 }
132 return SimPowerDistributionData[module].current[channel];
133}
134void HAL_GetPowerDistributionAllChannelCurrents(
135 HAL_PowerDistributionHandle handle, double* currents,
136 int32_t currentsLength, int32_t* status) {
137 auto module = hal::can::GetCANModuleFromHandle(handle, status);
138 if (*status != 0) {
139 return;
140 }
141
142 auto& data = SimPowerDistributionData[module];
143 int toCopy = (std::min)(currentsLength, kNumPDSimChannels);
144 for (int i = 0; i < toCopy; i++) {
145 currents[i] = data.current[i];
146 }
147}
148double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle,
149 int32_t* status) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800150 auto module = hal::can::GetCANModuleFromHandle(handle, status);
151 if (*status != 0) {
152 return 0.0;
153 }
154
155 double total = 0.0;
156 auto& data = SimPowerDistributionData[module];
157 for (int i = 0; i < kNumPDSimChannels; i++) {
158 total += data.current[i];
159 }
160 return total;
Austin Schuh812d0d12021-11-04 20:16:48 -0700161}
162double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle,
163 int32_t* status) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800164 double voltage = HAL_GetPowerDistributionVoltage(handle, status);
165 double current = HAL_GetPowerDistributionTotalCurrent(handle, status);
166 return voltage * current;
Austin Schuh812d0d12021-11-04 20:16:48 -0700167}
168double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
169 int32_t* status) {
170 return 0.0;
171}
172void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
173 int32_t* status) {}
174void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle,
175 int32_t* status) {}
176void HAL_SetPowerDistributionSwitchableChannel(
177 HAL_PowerDistributionHandle handle, HAL_Bool state, int32_t* status) {}
178
179HAL_Bool HAL_GetPowerDistributionSwitchableChannel(
180 HAL_PowerDistributionHandle handle, int32_t* status) {
181 return false;
182}
Austin Schuh75263e32022-02-22 18:05:32 -0800183
184void HAL_GetPowerDistributionVersion(HAL_PowerDistributionHandle handle,
185 HAL_PowerDistributionVersion* version,
186 int32_t* status) {}
187
188void HAL_GetPowerDistributionFaults(HAL_PowerDistributionHandle handle,
189 HAL_PowerDistributionFaults* faults,
190 int32_t* status) {}
191
192void HAL_GetPowerDistributionStickyFaults(
193 HAL_PowerDistributionHandle handle,
194 HAL_PowerDistributionStickyFaults* stickyFaults, int32_t* status) {}
Austin Schuh812d0d12021-11-04 20:16:48 -0700195} // extern "C"