blob: cd1ac5fcb52aac519120e914855d3ae8167ecd15 [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.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#pragma once
6
7#include <stdint.h>
8
Austin Schuh812d0d12021-11-04 20:16:48 -07009#include <string>
10
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include "PortsInternal.h"
12#include "hal/AnalogTrigger.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include "hal/handles/DigitalHandleResource.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014
15namespace hal {
16/**
17 * MXP channels when used as digital output PWM are offset from actual value
18 */
19constexpr int32_t kMXPDigitalPWMOffset = 6;
20
21constexpr int32_t kExpectedLoopTiming = 40;
22
23/**
24 * kDefaultPwmPeriod is in ms
25 *
26 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all
27 * devices
28 * - 20ms periods seem to be desirable for Vex Motors
29 * - 20ms periods are the specified period for HS-322HD servos, but work
30 * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums
31 * and get hot; by 5.0ms the hum is nearly continuous
32 * - 10ms periods work well for Victor 884
33 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
34 * controllers. Due to the shipping firmware on the Jaguar, we can't run the
35 * update period less than 5.05 ms.
36 *
37 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period
38 * scaling is implemented as an output squelch to get longer periods for old
39 * devices.
40 */
41constexpr float kDefaultPwmPeriod = 5.05f;
42/**
43 * kDefaultPwmCenter is the PWM range center in ms
44 */
45constexpr float kDefaultPwmCenter = 1.5f;
46/**
47 * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
48 */
49constexpr int32_t kDefaultPwmStepsDown = 1000;
50constexpr int32_t kPwmDisabled = 0;
51
52struct DigitalPort {
53 uint8_t channel;
54 bool configSet = false;
55 bool eliminateDeadband = false;
56 int32_t maxPwm = 0;
57 int32_t deadbandMaxPwm = 0;
58 int32_t centerPwm = 0;
59 int32_t deadbandMinPwm = 0;
60 int32_t minPwm = 0;
Austin Schuh812d0d12021-11-04 20:16:48 -070061 std::string previousAllocation;
Brian Silverman8fce7482020-01-05 13:18:21 -080062};
63
64extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
65 kNumDigitalChannels + kNumPWMHeaders>*
66 digitalChannelHandles;
67
68/**
69 * Remap the digital source channel and set the module.
70 *
71 * If it's an analog trigger, determine the module from the high order routing
72 * channel else do normal digital input remapping based on channel number
73 * (MXP).
74 */
75bool remapDigitalSource(HAL_Handle digitalSourceHandle,
76 HAL_AnalogTriggerType analogTriggerType,
77 uint8_t& channel, uint8_t& module, bool& analogTrigger);
78
79/**
80 * Map DIO channel numbers from their physical number (10 to 26) to their
81 * position in the bit field.
82 */
83int32_t remapMXPChannel(int32_t channel);
84
85int32_t remapMXPPWMChannel(int32_t channel);
86
87int32_t GetDigitalInputChannel(HAL_DigitalHandle handle, int32_t* status);
88} // namespace hal