ASMESDC2018
/home/swerve/Github/sdc_2018/omni_robot/RCController6CH.cpp
Go to the documentation of this file.
1 
8 #include <Arduino.h>
9 
10 #include "Logger.h"
11 #include "RCController6CH.h"
12 
13 /* CONSTRUCTOR FUNCTIONS */
14 RCController6CH::RCController6CH(int pin_ch_1, void (*interrupt_func)(void)) {
15 
16  for (int pin = 0; pin < MAX_CHANNELS; pin++) {
17  *pins.PINS[pin] = pin_ch_1 + pin;
18  }
19 
20  setupController(interrupt_func);
21 
22 }
23 
24 RCController6CH::RCController6CH(int pin_ch_1, int pin_ch_2, int pin_ch_3,
25  int pin_ch_4, int pin_ch_5, int pin_ch_6, void (*interrupt_func)(void)) {
26 
27  pins.PIN_CH_1 = pin_ch_1;
28  pins.PIN_CH_2 = pin_ch_2;
29  pins.PIN_CH_3 = pin_ch_3;
30  pins.PIN_CH_4 = pin_ch_4;
31  pins.PIN_CH_5 = pin_ch_5;
32  pins.PIN_CH_6 = pin_ch_6;
33 
34  setupController(interrupt_func);
35 
36 }
37 
38 /* PUBLIC FUNCTIONS */
39 void RCController6CH::setupController(void (*interrupt_func)(void)) {
40 
41  for (int pin = 0; pin < MAX_CHANNELS; pin++) {
42  pinMode(*pins.PINS[pin], INPUT);
43  }
44 
45  _interval_timer.begin(*interrupt_func, CH_UPDATE_RATE);
46 
47  logger::displayInfo("RC controller has been setup");
48 
49 }
50 
52 
53  int channel_input;
54 
55  cli();
56  for (int pin = 0; pin < MAX_CHANNELS; pin++) {
57  channel_input = pulseIn(*pins.PINS[pin], HIGH, PULSE_IN_TIMEOUT);
58  if (channel_input == 0) { // if the controller is off, set input to zero and move to next input
59  *state.values[pin] = 0;
60  continue;
61  }
62 
63  channel_input = map(channel_input, CH_VALUE_MIN, CH_VALUE_MAX, CH_VALUE_MAP_MIN, CH_VALUE_MAP_MAX);
64  channel_input = constrain(channel_input, CH_VALUE_MAP_MIN, CH_VALUE_MAP_MAX);
65  *state.values[pin] = channel_input;
66  }
67  sei();
68 
69 }
70 
72 
73  String channel_info = "Channel Values: ";
74 
75  cli();
76  for (int pin = 0; pin < MAX_CHANNELS; pin++) {
77  channel_info += String(*state.values[pin]) + " (CH " + String(pin+1) + ") | ";
78  }
79  sei();
80 
81  channel_info = channel_info.substring(0, channel_info.length()-3);
82  logger::displayInfo(channel_info);
83 
84 }
85 
86 
void displayChannels(void)
Display all the current channel values to the serial monitor.
void displayInfo(String)
int displayError(int)
Definition: Logger.cpp:36
RCController6CH(int, void(*)())
Default constructor.
#define PULSE_IN_TIMEOUT
void setupController(void(*)())
Setup controller pins and timed interrupt to update channels.
Header file for RCController6CH class
RCController6CHState state
RCController6CHPins pins
#define CH_VALUE_MAX
#define MAX_CHANNELS
void updateChannels(void)
Update the channel values.
#define CH_VALUE_MAP_MIN
#define CH_VALUE_MIN
volatile int * values[6]
#define CH_VALUE_MAP_MAX
Header file for error codes