ASMESDC2018
/home/swerve/Github/sdc_2018/omni_robot/OmniRobot.cpp
Go to the documentation of this file.
1 
8 #include <Arduino.h>
9 #include <math.h>
10 
11 #include "Logger.h"
12 #include "OmniRobot.h"
13 
14 /* CONSTRUCTOR FUNCTIONS */
16  rc_controller(PIN_CH_1, &updateController),
21 
22  ready = true;
23 
24 }
25 
26 /* PUBLIC FUNCTIONS */
28 
29  // Initialize variables
30  int x_input; // x input in the body frame of the robot
31  int y_input; // y input in the body frame of the robot
32  int spin_input; // spin input in the body frame of the robot
33 
34  int max_wheel_speed; // max speed of the three wheels
35 
36  // Get x, y, and spin input from controller
37  cli();
38  x_input = rc_controller.state.value_ch_2;
39  y_input = -rc_controller.state.value_ch_1;
40  spin_input = -rc_controller.state.value_ch_4;
41  sei();
42 
43  // Apply dead zone for controller
44  _applyDeadZone(&x_input, &y_input, &spin_input);
45 
46  // Calculate speed and direction in the body frame
47  state.body_spin = int(SPIN_GAIN * spin_input);
48  state.body_speed = constrain(sqrt(x_input*x_input + y_input*y_input), SPEED_MIN, SPEED_MAX);
49  state.body_direction = atan2(y_input, x_input);
50 
51  // Calcualte individual wheel speeds
52  state.wheel_speed_left = int(state.body_speed * cos(-150.0*DEG_TO_RAD + state.body_direction)) + state.body_spin;
53  state.wheel_speed_right = int(state.body_speed * cos( -30.0*DEG_TO_RAD + state.body_direction)) + state.body_spin;
54  state.wheel_speed_tail = int(state.body_speed * cos(-270.0*DEG_TO_RAD + state.body_direction)) + state.body_spin;
55 
56  // Check if any wheels are maxed out and adjust accordingly to make sure max wheel speed is 100
58  if (max_wheel_speed > SPEED_MAX) {
59  double speed_scale = ((SPEED_MAX - state.body_spin) / double(max_wheel_speed - state.body_spin));
63  }
64 
65  // Move motors at desired speeds
69 
70 }
71 
73 
74  logger::displayInfo("Robot State: " + String(state.wheel_speed_left) + " (left) | " +
75  String(state.wheel_speed_right) + " (right) | " + String(state.wheel_speed_tail) +
76  " (tail) | " + String(state.body_speed) + " (body speed) | " + String(state.body_direction)
77  + " (body direction) | " + String(state.body_spin) + " (body spin)");
78 
79 }
80 
82 
84 
85 }
86 
88 
90 
91 }
92 
94 
96 
97 }
98 
99 /* PRIVATE FUNCTIONS */
100 void OmniRobot::_applyDeadZone(int *input_1, int *input_2, int *input_3) {
101 
102  if (abs(*input_1) < DEAD_ZONE) {
103  *input_1 = 0;
104  }
105  if (abs(*input_2) < DEAD_ZONE) {
106  *input_2 = 0;
107  }
108  if (abs(*input_3) < DEAD_ZONE) {
109  *input_3 = 0;
110  }
111 
112 }
113 
114 
void displayChannels(void)
Display all the current channel values to the serial monitor.
RCController6CH rc_controller
Definition: OmniRobot.h:106
void displayInfo(String)
int displayError(int)
Definition: Logger.cpp:36
#define PIN_B_LEFT
Definition: OmniRobot.h:25
Motor drive_tail
Definition: OmniRobot.h:112
volatile int wheel_speed_right
Definition: OmniRobot.h:49
bool ready
Definition: OmniRobot.h:56
#define PIN_PWM_RIGHT
Definition: OmniRobot.h:17
#define PIN_RIGHT_GREEN
Definition: OmniRobot.h:33
void run(int)
Run the motor at the desired speed and direction or position.
Definition: Motor.cpp:65
#define PIN_RIGHT_RED
Definition: OmniRobot.h:32
Motor drive_right
Definition: OmniRobot.h:111
Motor drive_left
Definition: OmniRobot.h:110
#define PIN_B_TAIL
Definition: OmniRobot.h:21
#define SPIN_GAIN
Definition: OmniRobot.h:39
void updateController(void)
#define PIN_CH_1
Definition: OmniRobot.h:27
RCController6CHState state
#define PIN_B_RIGHT
Definition: OmniRobot.h:23
volatile int body_spin
Definition: OmniRobot.h:44
void updateLED(void)
Updates LED colors based on robot state.
Definition: OmniRobot.cpp:93
#define DEAD_ZONE
Definition: OmniRobot.h:38
volatile int wheel_speed_left
Definition: OmniRobot.h:48
void displayRCChannels(void)
Displays all the values from the RC controller.
Definition: OmniRobot.cpp:81
#define PIN_LEFT_RED
Definition: OmniRobot.h:29
void runRobotModel(void)
Executes the robot model based on input from the RC controller.
Definition: OmniRobot.cpp:27
#define PIN_LEFT_BLUE
Definition: OmniRobot.h:31
void displayRobotState(void)
Displays the robot state.
Definition: OmniRobot.cpp:72
#define PIN_LEFT_GREEN
Definition: OmniRobot.h:30
void updateChannels(void)
Update the channel values.
#define PIN_A_LEFT
Definition: OmniRobot.h:24
OmniRobot(void)
Default constructor.
Definition: OmniRobot.cpp:15
#define PIN_A_RIGHT
Definition: OmniRobot.h:22
volatile int wheel_speed_tail
Definition: OmniRobot.h:50
#define PIN_RIGHT_BLUE
Definition: OmniRobot.h:34
#define PIN_PWM_LEFT
Definition: OmniRobot.h:16
volatile int value_ch_2
OmniRobotState state
Definition: OmniRobot.h:104
#define PIN_A_TAIL
Definition: OmniRobot.h:20
#define MOTOR_TYPE_DC
Definition: Motor.h:17
Header file for error codes
void setState(int, bool blink=false, int side=SIDE_BOTH)
Definition: LED.cpp:50
#define COLOR_GREEN
Definition: LED.h:16
Header file for OmniRobot class
#define PIN_PWM_TAIL
Definition: OmniRobot.h:18
#define SPEED_MAX
Definition: OmniRobot.h:37
#define SPEED_MIN
Definition: OmniRobot.h:36
volatile int value_ch_1
volatile int value_ch_4
volatile double body_direction
Definition: OmniRobot.h:46
volatile double body_speed
Definition: OmniRobot.h:45
void updateRCChannels(void)
Updates the RC controller values.
Definition: OmniRobot.cpp:87