blob: ad4f45b97b9089439979f0e0aa85fda9575675b1 [file] [log] [blame]
Brian Silverman78670262014-01-17 23:40:47 -08001#include "cape/robot.h"
2
3#include <STM32F2XX.h>
4
5#include "cape/encoder.h"
6#include "cape/analog.h"
7#include "cape/digital.h"
8#include "cape/util.h"
9
Brian Silvermanfac5c292014-02-17 15:26:57 -080010// TIM11.1 on PB9, aka digital input 6.
Brian Silverman78670262014-01-17 23:40:47 -080011static volatile uint32_t ultrasonic_pulse_length = 0;
12
Brian Silvermanfac5c292014-02-17 15:26:57 -080013typedef struct {
14 uint32_t posedges, negedges;
15} EdgeCounts;
16
17#define COPY_EDGE_COUNTS(edge_counts, hall_effect_edges) \
Brian Silverman258349f2014-02-17 21:38:53 -080018 hall_effect_edges.posedges = edge_counts.negedges; \
19 hall_effect_edges.negedges = edge_counts.posedges;
Brian Silvermanfac5c292014-02-17 15:26:57 -080020
21#define HALL_CAPTURE(num, edges, encoder, capture) \
22 void digital_capture_##num##P(void) { \
23 ++edges.posedges; \
24 capture.posedge = encoder_read(encoder); \
25 } \
26 void digital_capture_##num##N(void) { \
27 ++edges.negedges; \
28 capture.negedge = encoder_read(encoder); \
29 }
30#define HALL_CAPTURE_DECL(num, edges, encoder, capture) \
31 static volatile EdgeCounts edges = {0, 0}; \
32 HALL_CAPTURE(num, edges, encoder, capture)
33
34static volatile struct {
35 int32_t posedge, negedge;
36} pusher_distal_captures, pusher_proximal_captures;
37
Brian Silverman258349f2014-02-17 21:38:53 -080038#define SHOOTER(plunger_num, pusher_distal_num, pusher_proximal_num, \
39 latch_num, encoder) \
40 HALL_CAPTURE_DECL(pusher_distal_num, pusher_distal, encoder, \
41 pusher_distal_captures); \
42 HALL_CAPTURE_DECL(pusher_proximal_num, pusher_proximal, encoder, \
43 pusher_proximal_captures); \
44 static inline void fill_shooter_values(struct DataStruct *packet) { \
45 digital_capture_disable(pusher_distal_num); \
46 digital_capture_disable(pusher_proximal_num); \
47 packet->main.shooter_position = encoder_read(encoder); \
48 packet->main.pusher_distal_posedge_position = \
49 pusher_distal_captures.negedge; \
50 packet->main.pusher_proximal_posedge_position = \
51 pusher_proximal_captures.negedge; \
52 packet->main.bools.pusher_distal = !digital_read(pusher_distal_num); \
53 packet->main.bools.pusher_proximal = !digital_read(pusher_proximal_num); \
54 COPY_EDGE_COUNTS(pusher_distal, packet->main.pusher_distal); \
55 COPY_EDGE_COUNTS(pusher_proximal, packet->main.pusher_proximal); \
56 digital_capture_enable(pusher_distal_num); \
57 digital_capture_enable(pusher_proximal_num); \
58 packet->main.bools.plunger = !digital_read(plunger_num); \
59 packet->main.bools.latch = !digital_read(latch_num); \
Brian Silvermanfac5c292014-02-17 15:26:57 -080060 }
61
Brian Silvermanfac5c292014-02-17 15:26:57 -080062typedef struct {
63 int32_t posedge, negedge;
64 EdgeCounts front, calibration, back;
65} SingleClawCaptures;
66
67#define CLAW(front_num, calibration_num, back_num, name, encoder) \
68 static volatile SingleClawCaptures name = {0, 0, {0, 0}, {0, 0}, {0, 0}}; \
69 HALL_CAPTURE(front_num, name.front, encoder, name); \
70 HALL_CAPTURE(calibration_num, name.calibration, encoder, name); \
71 HALL_CAPTURE(back_num, name.back, encoder, name); \
72 static inline void fill_##name##_values(struct DataStruct *packet) { \
73 digital_capture_disable(front_num); \
74 digital_capture_disable(calibration_num); \
75 digital_capture_disable(back_num); \
76 packet->main.name.position = encoder_read(encoder); \
Brian Silverman258349f2014-02-17 21:38:53 -080077 packet->main.name.negedge_position = name.posedge; \
78 packet->main.name.posedge_position = name.negedge; \
79 packet->main.name.bools.front = !digital_read(front_num); \
80 packet->main.name.bools.calibration = !digital_read(calibration_num); \
81 packet->main.name.bools.back = !digital_read(back_num); \
Brian Silvermanfac5c292014-02-17 15:26:57 -080082 COPY_EDGE_COUNTS(name.front, packet->main.name.front); \
83 COPY_EDGE_COUNTS(name.calibration, packet->main.name.calibration); \
84 COPY_EDGE_COUNTS(name.back, packet->main.name.back); \
85 digital_capture_enable(front_num); \
86 digital_capture_enable(calibration_num); \
87 digital_capture_enable(back_num); \
88 }
89
Brian Silverman258349f2014-02-17 21:38:53 -080090CLAW(1, 2, 0, top_claw, 2);
91CLAW(10, 11, 9, bottom_claw, 7);
Brian Silverman30a65dd2014-02-17 17:32:14 -080092SHOOTER(7, 5, 4, 8, 0)
Brian Silvermanfac5c292014-02-17 15:26:57 -080093
Brian Silverman78670262014-01-17 23:40:47 -080094void TIM1_TRG_COM_TIM11_IRQHandler(void) {
95 TIM11->SR = ~TIM_SR_CC1IF;
Brian Silverman258349f2014-02-17 21:38:53 -080096 TIM11->CR1 = 0;
Brian Silverman78670262014-01-17 23:40:47 -080097 ultrasonic_pulse_length = TIM11->CCR1;
Brian Silverman258349f2014-02-17 21:38:53 -080098 ultrasonic_pulse_length = 1;
99 TIM11->CNT = 0;
Brian Silverman78670262014-01-17 23:40:47 -0800100}
101
102void robot_init(void) {
Brian Silvermanc47182f2014-02-16 21:43:36 -0800103 gpio_setup_alt(GPIOB, 9, 3);
Brian Silverman78670262014-01-17 23:40:47 -0800104 RCC->APB2ENR |= RCC_APB2ENR_TIM11EN;
Brian Silverman5f4bfe02014-02-18 00:57:53 -0800105 TIM11->CR1 = TIM_CR1_URS;
Brian Silverman78670262014-01-17 23:40:47 -0800106 TIM11->SMCR = 5 << 4 /* TI1 */ |
Brian Silverman258349f2014-02-17 21:38:53 -0800107 6 << 0 /* start on rising edge */;
Brian Silverman78670262014-01-17 23:40:47 -0800108 TIM11->DIER = TIM_DIER_CC1IE;
109 TIM11->CCMR1 = TIM_CCMR1_CC1S_0 /* input pin 1 -> timer input 1 */;
Brian Silverman258349f2014-02-17 21:38:53 -0800110 TIM11->CCER = TIM_CCER_CC1P /* go on falling edge */ | TIM_CCER_CC1E;
Brian Silverman78670262014-01-17 23:40:47 -0800111 TIM11->EGR = TIM_EGR_UG;
112 TIM11->PSC = 1200 - 1; // 100kHZ timer
113 TIM11->CR1 |= TIM_CR1_CEN;
114 NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, 3);
115 NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn);
116}
117
118void robot_fill_packet(struct DataStruct *packet) {
Brian Silvermanfac5c292014-02-17 15:26:57 -0800119 packet->main.left_drive = encoder_read(6);
120 packet->main.right_drive = encoder_read(5);
Austin Schuh809c2562014-03-02 11:50:19 -0800121 packet->main.low_left_drive_hall = analog_get(7);
122 packet->main.low_right_drive_hall = analog_get(0);
123 packet->main.high_left_drive_hall = analog_get(6);
124 packet->main.high_right_drive_hall = analog_get(1);
Brian Silvermanfac5c292014-02-17 15:26:57 -0800125
126 packet->main.ultrasonic_pulse_length = ultrasonic_pulse_length;
127
128 fill_top_claw_values(packet);
129 fill_bottom_claw_values(packet);
Brian Silverman258349f2014-02-17 21:38:53 -0800130 fill_shooter_values(packet);
Brian Silverman78670262014-01-17 23:40:47 -0800131}