Revision 1461
updated all the library code to have sensible _init behavior.
Almost all of the library components have a global variable which gets set after init and the functions inside will fail with an error code if init has not been called. Also, the init functions themselves check this variable and will bail out without doing any damage if that init has already been called
motor.c | ||
---|---|---|
43 | 43 |
* @{ |
44 | 44 |
**/ |
45 | 45 |
|
46 |
unsigned char motors_initd=0; |
|
47 |
|
|
46 | 48 |
/** |
47 | 49 |
* Initializes both motors so that they can be used with future |
48 | 50 |
* calls to motor1_set and motor2_set. |
49 | 51 |
* |
52 |
* @return 0 if init succesfull, an error code otherwise |
|
53 |
* |
|
50 | 54 |
* @see motors_off, motor1_set, motor2_set |
51 | 55 |
**/ |
52 |
void motors_init( void ) { |
|
56 |
int motors_init( void ) { |
|
57 |
|
|
58 |
if(motors_initd) |
|
59 |
return ERROR_INIT_ALREADY_INITD; |
|
60 |
|
|
61 |
|
|
53 | 62 |
// Configure counter such that we use phase correct |
54 | 63 |
// PWM with 8-bit resolution |
55 | 64 |
PORTA &= 0x0F; |
... | ... | |
65 | 74 |
OCR1AL=0; |
66 | 75 |
OCR1BH=0; |
67 | 76 |
OCR1BL=0; |
77 |
|
|
78 |
motors_initd=1; |
|
79 |
return 0; |
|
68 | 80 |
} |
69 | 81 |
|
70 | 82 |
/** |
... | ... | |
74 | 86 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
75 | 87 |
* @param speed The speed the motor will run at, in the range 0-255. |
76 | 88 |
* |
89 |
* @return 0 if init succesfull, an error code otherwise |
|
90 |
* |
|
77 | 91 |
* @see motor_r_set, motors_init |
78 | 92 |
**/ |
79 |
void motor_l_set(int direction, int speed) { |
|
93 |
int motor_l_set(int direction, int speed) { |
|
94 |
if(!motors_initd) |
|
95 |
return ERROR_LIBRARY_NOT_INITD; |
|
80 | 96 |
|
81 | 97 |
if(direction == 0) { |
82 | 98 |
// turn off PWM first if switching directions |
... | ... | |
98 | 114 |
|
99 | 115 |
// Set the timer to count up to speed, an 8-bit value |
100 | 116 |
OCR1AL = speed; |
117 |
|
|
118 |
return 0; |
|
101 | 119 |
} |
102 | 120 |
|
103 | 121 |
/** |
... | ... | |
107 | 125 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
108 | 126 |
* @param speed The speed the motor will run at, in the range 0-255. |
109 | 127 |
* |
128 |
* @return 0 if init succesfull, an error code otherwise |
|
129 |
* |
|
110 | 130 |
* @see motor_l_set, motors_init |
111 | 131 |
**/ |
112 |
void motor_r_set(int direction, int speed) { |
|
132 |
int motor_r_set(int direction, int speed) { |
|
133 |
if(!motors_initd) |
|
134 |
return ERROR_LIBRARY_NOT_INITD; |
|
135 |
|
|
113 | 136 |
if(direction == 0) { |
114 | 137 |
// PORTD |= 0x20; |
115 | 138 |
// PORTD &= 0x7F; |
... | ... | |
131 | 154 |
PORTA = (PORTA & 0x3F) | 0x40; |
132 | 155 |
} |
133 | 156 |
OCR1BL = speed; |
157 |
|
|
158 |
return 0; |
|
134 | 159 |
} |
135 | 160 |
|
136 | 161 |
/** |
... | ... | |
140 | 165 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
141 | 166 |
* @param speed The speed the motor will run at, in the range 0-255. |
142 | 167 |
* |
168 |
* @return 0 if init succesfull, an error code otherwise |
|
169 |
* |
|
143 | 170 |
* @see motor2_set, motors_init |
144 | 171 |
**/ |
145 |
void motor1_set(int direction, int speed) {
|
|
146 |
motor_l_set(direction, speed);
|
|
172 |
int motor1_set(int direction, int speed) {
|
|
173 |
return motor_l_set(direction, speed);
|
|
147 | 174 |
} |
148 | 175 |
|
149 | 176 |
/** |
... | ... | |
153 | 180 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
154 | 181 |
* @param speed The speed the motor will run at, in the range 0-255. |
155 | 182 |
* |
183 |
* @return 0 if init succesfull, an error code otherwise |
|
184 |
* |
|
156 | 185 |
* @see motor2_set, motors_init |
157 | 186 |
**/ |
158 |
void motor2_set(int direction, int speed) {
|
|
159 |
motor_r_set(direction, speed);
|
|
187 |
int motor2_set(int direction, int speed) {
|
|
188 |
return motor_r_set(direction, speed);
|
|
160 | 189 |
} |
161 | 190 |
|
162 | 191 |
|
163 | 192 |
/** |
164 | 193 |
* Turns off both motors. |
165 | 194 |
* |
195 |
* @return 0 if init succesfull, an error code otherwise |
|
196 |
* |
|
166 | 197 |
* @see motors_init |
167 | 198 |
**/ |
168 |
void motors_off( void ) { |
|
199 |
int motors_off( void ) { |
|
200 |
if(!motors_initd) |
|
201 |
return ERROR_LIBRARY_NOT_INITD; |
|
202 |
|
|
169 | 203 |
OCR1AL = 0x0; |
170 | 204 |
OCR1BL = 0x0; |
205 |
|
|
206 |
return 0; |
|
171 | 207 |
} |
172 | 208 |
|
173 | 209 |
/**@}**///end defgroup |
Also available in: Unified diff