forked from UWARG/RC-Car-Bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWM.c
107 lines (99 loc) · 3.37 KB
/
PWM.c
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
107
/*
* File: PWM.c
*
* Created on August 21, 2014, 11:40 PM
*/
#include "PWM.h"
#include "OutputCompare.h"
#include "InputCapture.h"
#include "debug.h"
char initialized = 0;
int pwmArray[NUM_CHANNELS];
float scaleFactorIn[NUM_CHANNELS];
int offsetIn[NUM_CHANNELS];
float scaleFactorOut[NUM_CHANNELS];
int offsetOut[NUM_CHANNELS];
void initPWM(char inputChannels, char outputChannels){
initIC(inputChannels);
int i = 0;
for (i = 0; i < NUM_CHANNELS; i++){
// Use default scale factor values
scaleFactorIn[i] = MAX_PWM/(float)(UPPER_PWM - MIDDLE_PWM); // We know that MAX_PWM = (UPPER_PWM - MIDDLE_PWM)/2 for a factor of 1.
// Use default offset values
offsetIn[i] = MIDDLE_PWM;
// Use default scale factor values
scaleFactorOut[i] = (float)(UPPER_PWM - MIDDLE_PWM)/MAX_PWM; // We know that MAX_PWM = (UPPER_PWM - MIDDLE_PWM) for a factor of 1.
// Use default offset values
offsetOut[i] = MIDDLE_PWM;
}
initOC(outputChannels);
initialized = 1;
}
void PWMInputCalibration(unsigned int channel, float signalScaleFactor, unsigned int signalOffset){
if (channel > 0 && channel <= NUM_CHANNELS){ //Check if channel number is valid
scaleFactorIn[channel - 1] = signalScaleFactor;
offsetIn[channel - 1] = signalOffset;
}
else { //Invalid Channel
//Display Error Message
error("Invalid PWM channel");
}
}
void PWMOutputCalibration(unsigned int channel, float signalScaleFactor, unsigned int signalOffset){
if (channel > 0 && channel <= NUM_CHANNELS){ //Check if channel number is valid
scaleFactorOut[channel - 1] = signalScaleFactor;
offsetOut[channel - 1] = signalOffset;
}
else { //Invalid Channel
//Display Error Message
error("Invalid PWM channel");
}
}
int getPWM(unsigned int channel){
if (initialized && channel > 0 && channel <= NUM_CHANNELS){ //Is the Input Initialized?
return (getICValue(channel) - offsetIn[channel - 1]) * scaleFactorIn[channel - 1];
}
else { //Not initialized or invalid channel
//Display Error Message
error("PWM not initialized or invalid channel specified");
//Return 0
return 0;
}
}
int* getPWMArray(){
if (initialized){ //Is the Input Initialized?
unsigned int* icArray = getICValues();
int i = 0;
for (i = 0; i < NUM_CHANNELS; i++){
pwmArray[i] = (int)((int)icArray[i] - offsetIn[i]) * scaleFactorIn[i];
}
return pwmArray;
}
else { //Not initialized
//Display Error Message
error("PWM not initialized");
//Return 0
return 0;
}
}
void setPWM(unsigned int channel, int pwm){
if (initialized && channel > 0 && channel <= NUM_CHANNELS){ //Is the Input Initialized?
setOCValue(channel, (int)(pwm * scaleFactorOut[channel - 1] + offsetOut[channel - 1]));
}
else { //Not initialized or invalid channel
//Display Error Message
error("PWM not initialized or invalid channel specified");
}
}
void setPWMArray(int* ocArray){
if (initialized){ //Is the Input Initialized?
int i = 0;
for (i = 0; i < NUM_CHANNELS; i++){
setOCValue(i,(int)(ocArray[i] * scaleFactorOut[i] + offsetOut[i]));
}
}
else { //Not initialized
//Display Error Message
error("PWM not initialized.");
}
}