-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.c
More file actions
107 lines (81 loc) · 3.32 KB
/
platform.c
File metadata and controls
107 lines (81 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <xc.h>
#include "mcc_generated_files/adcc.h"
#include "platform.h"
void pin_init(void) {
// LEDS
TRISA2 = 0; // set red LED pin as output
ANSELA2 = 0; // enable digital input buffer (Useful for reading the LED state)
LATA2 = LED_ON; // start with 5V power enabled
TRISA1 = 0; // set blue LED pin as output
ANSELA1 = 0; // enable digital input buffer (Useful for reading the LED state)
LATA1 = !LED_ON; // start with charging disabled
TRISA0 = 0; // set white LED pin as output
ANSELA0 = 0; // enable digital input buffer (Useful for reading the LED state)
LATA0 = !LED_ON;
// Rocket power lines
LATA3 = CAN_5V_ON;
TRISA3 = 0; // allow 5V current line to be toggle-able
TRISB1 = 1; // set 13V current draw (battery) to be input
ANSELB1 = 1; // enable analog reading
TRISB0 = 1; // set 5V current draw (payload logic + motor) to be input
ANSELB0 = 1; // enable analog reading
TRISC7 = 1; // set +BATT current draw (battery) to be input
ANSELC7 = 1; // enable analog reading
// Battery charger
LATA5 = !CHG_BATT_ON; // start with charging disabled
TRISA5 = 0; // allow battery charging to be toggle-able
TRISA4 = 1; // set battery charging current to be input
ANSELA4 = 1; // enable analog reading
// Voltage health
TRISC2 = 1; // set +BATT voltage to be input
ANSELC2 = 1; // enable analog reading
TRISC3 = 1; // set +13V voltage to be input
ANSELC3 = 1; // enable analog reading
}
void RED_LED_SET(bool value) {
LATA2 = !value ^ LED_ON;
}
void BLUE_LED_SET(bool value) {
LATA1 = !value ^ LED_ON;
}
void WHITE_LED_SET(bool value) {
LATA0 = !value ^ LED_ON;
}
void CAN_5V_SET(bool value) {
LATA3 = !value ^ CAN_5V_ON;
}
void BATTERY_CHARGER_EN(bool value) {
LATA5 = !value ^ CHG_BATT_ON;
}
// this code is for low pass filter stuff for current
// the following code was yoinked from cansw_arming
// zach derived the equation alpha = (Fs*T/5)/ 1 + (Fs*T/5)
// where Fs = sampling frequency and T = response time
// response time is equivalent to 5*tau or 5/2pi*Fc, where Fc is cutoff frequency
double alpha_low = LOW_PASS_ALPHA(LOW_PASS_RESPONSE_TIME);
double low_pass_curr_batt = 0;
double low_pass_curr_motor = 0;
double low_pass_curr_13v = 0;
double low_pass_curr_5v = 0;
//i think this is needed for 13V BATT Motor and 5V current readings? not sure tho
void update_batt_curr_low_pass(void) {
double new_curr_reading = ADCC_GetSingleConversion(channel_BATT_CURR) / CURR_BATT_RESISTOR;
low_pass_curr_batt = alpha_low * low_pass_curr_batt + (1.0 - alpha_low) * new_curr_reading;
}
uint16_t get_batt_curr_low_pass(void) {
return (uint16_t)low_pass_curr_batt;
}
void update_13v_curr_low_pass(void) {
double new_curr_reading = ADCC_GetSingleConversion(channel_POWER_V13) * CONVERSION_ADC_TO_V / CURR_13V_RESISTOR;
low_pass_curr_13v = alpha_low * low_pass_curr_13v + (1.0 - alpha_low) * new_curr_reading;
}
uint16_t get_13v_curr_low_pass(void) {
return (uint16_t)low_pass_curr_13v;
}
void update_5v_curr_low_pass(void) {
double new_curr_reading = ADCC_GetSingleConversion(channel_POWER_V5) * CONVERSION_ADC_TO_V / CURR_5V_RESISTOR;
low_pass_curr_5v = alpha_low * low_pass_curr_5v + (1.0 - alpha_low) * new_curr_reading;
}
uint16_t get_5v_curr_low_pass(void) {
return (uint16_t)low_pass_curr_5v;
}