EE445M RTOS
Taken at the University of Texas Spring 2015
drivepp.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset: 4; -*- */
2 #include "drivepp.hpp"
3 #include "math.hpp"
4 #include "ir.hpp"
5 #include "motorpp.hpp"
6 
7 #include "driverlib/gpio.h"
8 #include "inc/hw_memmap.h"
9 
11 
12 drive::drive(motor* left, motor* right, percent_t speed, Direction direction) {
13 
14  this->left = left;
15  this->right = right;
16  set(speed, direction);
18 }
19 
20 void drive::stop() {
21 
22  left->stop();
23  right->stop();
24 }
25 
26 void drive::start() {
27 
28  left->start();
29  right->start();
30 }
31 
33 
34  left->set(speed, FORWARD);
35  right->set(speed, FORWARD);
36 }
37 
39 
40  left->set(speed, BACKWARD);
41  right->set(speed, BACKWARD);
42 }
43 
44 void drive::set(percent_t speed, Direction dir) {
45 
46  left->set(speed, dir);
47  right->set(speed, dir);
48 }
49 
50 void drive::steer(uint32_t left_sens, uint32_t left_front_sens,
51  uint32_t right_sens, uint32_t right_front_sens,
52  uint32_t front_sens) {
53 
54  /* todo: feed the lf/f, rf/r data here for porportional control of
55  * the motors. the side with the larger coefficient slows more */
56 
57  Direction dir = FORWARD;
58 
59 
60  left_sens = 0;
61  right_sens = 0;
62  front_sens = 0;
63 
64  /* if (last_counter >= steps_to_wait) { */
65  /* last_counter = 0; */
66 
67  /* if (abs(last_left_sens - left_sens) > reset_thresh) { */
68  /* integral_oblique_error = 0; */
69  /* integral_side_error = 0; */
70  /* GPIOPinWrite(GPIO_PORTF_BASE, PIN_RED, PIN_RED ^ GPIOPinRead(GPIO_PORTF_BASE, PIN_RED)); */
71  /* } else if(abs(last_left_front_sens - left_front_sens) > reset_thresh) { */
72  /* integral_oblique_error = 0; */
73  /* integral_side_error = 0; */
74  /* GPIOPinWrite(GPIO_PORTF_BASE, PIN_RED, PIN_RED ^ GPIOPinRead(GPIO_PORTF_BASE, PIN_RED)); */
75  /* } else if(abs(last_right_sens - right_sens) > reset_thresh) { */
76  /* integral_oblique_error = 0; */
77  /* integral_side_error = 0; */
78  /* GPIOPinWrite(GPIO_PORTF_BASE, PIN_RED, PIN_RED ^ GPIOPinRead(GPIO_PORTF_BASE, PIN_RED)); */
79  /* } else if(abs(last_right_front_sens - right_front_sens) > reset_thresh) { */
80  /* integral_oblique_error = 0; */
81  /* integral_side_error = 0; */
82  /* GPIOPinWrite(GPIO_PORTF_BASE, PIN_RED, PIN_RED ^ GPIOPinRead(GPIO_PORTF_BASE, PIN_RED)); */
83  /* } */
84 
85  /* last_left_sens = left_sens; */
86  /* last_left_front_sens = left_front_sens; */
87  /* last_right_sens = right_sens; */
88  /* last_right_front_sens = right_front_sens; */
89  /* } */
90 
91  int32_t left_speed = left->pwm_max_period/2;
92  int32_t right_speed = right->pwm_max_period/2;
93 
94  int32_t oblique_error = ((int32_t)left_front_sens - (int32_t)right_front_sens);
96 
97  int32_t side_error = ((int32_t)left_sens - (int32_t)right_sens);
99 
100  int32_t should_use_side_sensors = front_sens < 200;
101  should_use_side_sensors = 0;
102 
103  int32_t should_slow_down = front_sens < 200;
104 
105  //int32_t delta = oblique_error*kp_oblique_num/kp_oblique_denom +
106  // (should_use_side_sensors * side_error * kp_side_num/kp_side_denom) +
107  // (integral_oblique_error*ki_oblique_num/ki_oblique_denom) + (integral_side_error*ki_side_num/ki_side_denom);
108  int32_t delta = oblique_error*kp_oblique_num/kp_oblique_denom;
109  //+ (integral_oblique_error*ki_oblique_num/ki_oblique_denom);
110 
111  if (should_slow_down > 0) { delta /= 2; }
112 
113  left_speed += delta;
114  right_speed -= delta;
115 
116  left->set(left_speed, dir);
117  right->set(right_speed, dir);
118 
119  ++last_counter;
120 }
121 
123 
126 }
127 
128 /* Local Variables: */
129 /* firestarter: (compile "make -k -j32 -C ~/workspace/ee445m-labs/build/") */
130 /* End: */
static const int32_t kp_oblique_denom
Definition: drivepp.hpp:27
void set(percent_t speed, Direction dir)
Definition: drivepp.cpp:44
motor * right
Definition: drivepp.hpp:22
int32_t clamp(int32_t value, int32_t min, int32_t max)
Definition: math.cpp:5
static const uint16_t pwm_max_period
Definition: motorpp.hpp:75
void steer(uint32_t left_sens, uint32_t left_front_sens, uint32_t right_sens, uint32_t right_front_sens, uint32_t front_sens)
Definition: drivepp.cpp:50
int32_t percent_t
Definition: motorpp.hpp:15
drive()
Definition: drivepp.cpp:10
int32_t integral_oblique_error
Definition: drivepp.hpp:23
Direction
Definition: direction.hpp:8
void start(void)
Definition: motorpp.cpp:96
void backward(percent_t speed)
Definition: drivepp.cpp:38
int32_t integral_side_error
Definition: drivepp.hpp:24
void stop(void)
Definition: motorpp.cpp:45
motor * left
Definition: drivepp.hpp:21
int32_t last_counter
Definition: drivepp.hpp:38
void set(percent_t speed)
Definition: motorpp.cpp:67
void stop(void)
Definition: drivepp.cpp:20
void start(void)
Definition: drivepp.cpp:26
void reset_history()
Definition: drivepp.cpp:122
static const int32_t kp_oblique_num
Definition: drivepp.hpp:26
void forward(percent_t speed)
Definition: drivepp.cpp:32