blob: 65dcc851ec8dd42ec020777d03033f9770b33724 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2016-2017. 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 "HAL/AnalogTrigger.h"
15#include "HAL/ChipObject.h"
16#include "HAL/Ports.h"
17#include "HAL/Types.h"
18#include "HAL/handles/DigitalHandleResource.h"
19#include "HAL/handles/HandlesInternal.h"
20#include "PortsInternal.h"
21
22namespace hal {
23/**
24 * MXP channels when used as digital output PWM are offset from actual value
25 */
26constexpr int32_t kMXPDigitalPWMOffset = 6;
27
28constexpr int32_t kExpectedLoopTiming = 40;
29
30/**
31 * kDefaultPwmPeriod is in ms
32 *
33 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all
34 * devices
35 * - 20ms periods seem to be desirable for Vex Motors
36 * - 20ms periods are the specified period for HS-322HD servos, but work
37 * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums
38 * and get hot; by 5.0ms the hum is nearly continuous
39 * - 10ms periods work well for Victor 884
40 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
41 * controllers. Due to the shipping firmware on the Jaguar, we can't run the
42 * update period less than 5.05 ms.
43 *
44 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period
45 * scaling is implemented as an output squelch to get longer periods for old
46 * devices.
47 */
48constexpr double kDefaultPwmPeriod = 5.05;
49/**
50 * kDefaultPwmCenter is the PWM range center in ms
51 */
52constexpr double kDefaultPwmCenter = 1.5;
53/**
54 * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
55 */
56constexpr int32_t kDefaultPwmStepsDown = 1000;
57constexpr int32_t kPwmDisabled = 0;
58
59// Create a mutex to protect changes to the DO PWM config
60extern priority_recursive_mutex digitalPwmMutex;
61
62extern std::unique_ptr<tDIO> digitalSystem;
63extern std::unique_ptr<tRelay> relaySystem;
64extern std::unique_ptr<tPWM> pwmSystem;
65extern std::unique_ptr<tSPI> spiSystem;
66
67struct DigitalPort {
68 uint8_t channel;
69 bool configSet = false;
70 bool eliminateDeadband = false;
71 int32_t maxPwm = 0;
72 int32_t deadbandMaxPwm = 0;
73 int32_t centerPwm = 0;
74 int32_t deadbandMinPwm = 0;
75 int32_t minPwm = 0;
76};
77
78extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
79 kNumDigitalChannels + kNumPWMHeaders>
80 digitalChannelHandles;
81
82void initializeDigital(int32_t* status);
83bool remapDigitalSource(HAL_Handle digitalSourceHandle,
84 HAL_AnalogTriggerType analogTriggerType,
85 uint8_t& channel, uint8_t& module, bool& analogTrigger);
86int32_t remapSPIChannel(int32_t channel);
87int32_t remapMXPPWMChannel(int32_t channel);
88int32_t remapMXPChannel(int32_t channel);
89} // namespace hal