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