-
Notifications
You must be signed in to change notification settings - Fork 1
/
motor.c
206 lines (159 loc) · 3.78 KB
/
motor.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright 2008 Stephen English, Jeffrey Gough, Alexis Johnson,
Robert Spanton and Joanna A. Sun.
This file is part of the Formica robot firmware.
The Formica robot firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Formica robot firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the Formica robot firmware.
If not, see <http://www.gnu.org/licenses/>. */
#include "motor.h"
#include "device.h"
#include <signal.h>
#include "random.h"
#include "battery.h"
#include "ir.h"
#include "leds.h"
#include "bearing.h"
#include "time.h"
#include "behav/braitenberg.h"
#include "food.h"
#include "behav/parking.h"
#include "ir-tx.h"
motor_mode_t motor_mode = MOTOR_FWD;
uint8_t motor_r = 0;
uint8_t motor_l = 0;
void motor_rand_walk_change( void );
static uint8_t rand_walk_thresh = 0;
static bool random_walk_en = 0;
uint8_t MAX_SPEED = 8;
void random_walk_enable( void )
{
random_walk_en = TRUE;
}
void random_walk_disable( void )
{
random_walk_en = FALSE;
}
void motor_init( void )
{
/* Set all as inputs -- all motors off */
P1DIR &= ~M_ALL;
/* Outputs need to be low when we enable the motors */
P1OUT &= ~M_ALL;
/* Configure the watchdog timer into interval mode */
WDTCTL = WDTPW /* All writes to WDTCTL must have this password */
/* WDTHOLD = 0 -- Watchdog time is enabled */
/* WDTNMI = 0 -- RST pin is reset pin */
| WDTTMSEL /* Interval timer mode */
| WDTCNTCL /* Clear the timer */
/* WDTSSEL = 0 -- Source clock from SMCLK */
| WDTIS0; /* Divide clock by 8192 */
/* Enable the watchdog interrupt */
IE1 |= WDTIE;
}
interrupt (WDT_VECTOR) motor_wdt_isr(void)
{
static uint8_t count = 0;
/* The resulting motor configuration (to be ORed with P1DIR) */
uint8_t conf = 0;
static uint16_t cc = 0;
if( cc == 100 )
{
the_time++;
food_level++;
cc = 0;
if(random_walk_en)
{
static uint8_t j = 0;
if( j == rand_walk_thresh )
{
j = 0;
motor_rand_walk_change();
}
j++;
}
}
cc++;
// motor_mode = MOTOR_FWD;
//motor_l = motor_r = 6;
if( motor_mode == MOTOR_FWD )
{
MAX_SPEED = 8;
conf = M_FWD;
if( count >= motor_r )
conf |= M2;
if( count >= motor_l )
conf |= M1;
}
if( motor_mode == MOTOR_BK )
{
MAX_SPEED = 8;
conf = M_BK;
if( count >= motor_r )
conf &= ~M2;
if( count >= motor_l )
conf &= ~M1;
}
if( motor_mode == MOTOR_TURN_LEFT )
{
MAX_SPEED = 8;
motor_l = 1;
motor_r = 8;
conf = M_FWD;
if( count >= motor_r )
conf |= M2;
if( count >= motor_l )
conf |= M1;
}
if( motor_mode == MOTOR_TURN_RIGHT )
{
MAX_SPEED = 8;
motor_l = 8;
motor_r = 1;
conf = M_FWD;
if( count >= motor_r )
conf |= M2;
if( count >= motor_l )
conf |= M1;
}
motor_off();
P1DIR |= conf;
count++;
if( count == MAX_SPEED )
count = 0;
/* don't transmit data when parking */
ir_nudge();
}
void motor_rand_walk_change( void )
{
static uint8_t mode = 0;
motor_r = motor_l = RAND_WALK_SPEED;
mode = (random() >> 6) % 10;
switch(mode)
{
case 0:
case 1:
motor_mode = MOTOR_TURN_LEFT;
break;
case 2:
case 3:
motor_mode = MOTOR_TURN_RIGHT;
break;
case 4:
if( !hasfood() )
motor_mode = MOTOR_BK;
else
motor_mode = MOTOR_FWD;
break;
default:
motor_mode = MOTOR_FWD;
break;
}
rand_walk_thresh = (random() >> 5) + 1;
}