blob: d169c8ac638f8920a30b6ea5a19b4888a794945b [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include <stdint.h>
11
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include "PortsInternal.h"
13#include "hal/AnalogTrigger.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014#include "hal/handles/DigitalHandleResource.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080015
16namespace hal {
17/**
18 * MXP channels when used as digital output PWM are offset from actual value
19 */
20constexpr int32_t kMXPDigitalPWMOffset = 6;
21
22constexpr int32_t kExpectedLoopTiming = 40;
23
24/**
25 * kDefaultPwmPeriod is in ms
26 *
27 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all
28 * devices
29 * - 20ms periods seem to be desirable for Vex Motors
30 * - 20ms periods are the specified period for HS-322HD servos, but work
31 * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums
32 * and get hot; by 5.0ms the hum is nearly continuous
33 * - 10ms periods work well for Victor 884
34 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
35 * controllers. Due to the shipping firmware on the Jaguar, we can't run the
36 * update period less than 5.05 ms.
37 *
38 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period
39 * scaling is implemented as an output squelch to get longer periods for old
40 * devices.
41 */
42constexpr float kDefaultPwmPeriod = 5.05f;
43/**
44 * kDefaultPwmCenter is the PWM range center in ms
45 */
46constexpr float kDefaultPwmCenter = 1.5f;
47/**
48 * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
49 */
50constexpr int32_t kDefaultPwmStepsDown = 1000;
51constexpr int32_t kPwmDisabled = 0;
52
53struct DigitalPort {
54 uint8_t channel;
55 bool configSet = false;
56 bool eliminateDeadband = false;
57 int32_t maxPwm = 0;
58 int32_t deadbandMaxPwm = 0;
59 int32_t centerPwm = 0;
60 int32_t deadbandMinPwm = 0;
61 int32_t minPwm = 0;
62};
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