121 |
pfowler |
1 |
#include <Wire.h>
|
|
|
2 |
#include <math.h>
|
|
|
3 |
#include <VirtualWire.h>
|
|
|
4 |
#include "nunchuck_funcs.h"
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
const int MFORWARD = 1; // Set track to go forward
|
|
|
8 |
const int MREVERSE = 0; // Set track to reverse
|
|
|
9 |
|
|
|
10 |
const float SKID_FACTOR = 0.8; // Slow down inner track when skid steering
|
|
|
11 |
const float TURN_FACTOR = 0.33; // Inner track runs at n the speed of outside track
|
|
|
12 |
const int TURN_MAX = 102; // Maximum distance on WiiChuck diagonals
|
|
|
13 |
|
|
|
14 |
const int MIN_X = 25;
|
|
|
15 |
const int CENTER_X = 125; // Center of WiiChuck X value
|
|
|
16 |
const int MAX_X = 225;
|
|
|
17 |
|
|
|
18 |
const int MIN_Y = 30; // Minimum WiiChuck Y value
|
|
|
19 |
const int CENTER_Y = 130; // Center WiiChuck Y value
|
|
|
20 |
const int MAX_Y = 230; // Maximum WiiChuck Y value
|
|
|
21 |
|
|
|
22 |
const int ZONE_WIDTH = 40; // With on zone between movement modes
|
|
|
23 |
const int ZONE_HALF = ZONE_WIDTH / 2;
|
|
|
24 |
|
|
|
25 |
const int ZONE_X_LOW = CENTER_X - ZONE_HALF;
|
|
|
26 |
const int ZONE_X_HIGH = CENTER_X + ZONE_HALF;
|
|
|
27 |
const int ZONE_Y_LOW = CENTER_Y - ZONE_HALF;
|
|
|
28 |
const int ZONE_Y_HIGH = CENTER_Y + ZONE_HALF;
|
|
|
29 |
|
|
|
30 |
int ledPin = 13;
|
|
|
31 |
|
|
|
32 |
int loop_cnt=0;
|
|
|
33 |
char ttx_buffer[4];
|
|
|
34 |
byte joyy,joyx,zbut,cbut;
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
void setup()
|
|
|
38 |
{
|
|
|
39 |
Serial.begin(9600);
|
|
|
40 |
nunchuck_setpowerpins();
|
|
|
41 |
nunchuck_init(); // send the initilization handshake
|
|
|
42 |
|
|
|
43 |
vw_setup(2000); // VirtualWire Bits per sec
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
void loop()
|
|
|
47 |
{
|
|
|
48 |
if( loop_cnt > 10) { // every 100 msecs get new data
|
|
|
49 |
loop_cnt = 0;
|
|
|
50 |
|
|
|
51 |
nunchuck_get_data();
|
|
|
52 |
|
|
|
53 |
joyx = nunchuck_joyx();
|
|
|
54 |
joyy = nunchuck_joyy();
|
|
|
55 |
zbut = nunchuck_zbutton();
|
|
|
56 |
cbut = nunchuck_cbutton();
|
|
|
57 |
|
|
|
58 |
// Speed of each track
|
|
|
59 |
byte r_spd = 0;
|
|
|
60 |
byte l_spd = 0;
|
|
|
61 |
|
|
|
62 |
// Direction of each track
|
|
|
63 |
byte r_dir = 1;
|
|
|
64 |
byte l_dir = 1;
|
|
|
65 |
|
|
|
66 |
byte options = B00000000;
|
|
|
67 |
// -------0 -> Right track direction ( 1=F, 0=R )
|
|
|
68 |
// ------0- -> Left track direction ( 1=F, 0=R )
|
|
|
69 |
|
|
|
70 |
// *1* Reverse && in dead zone of X
|
|
|
71 |
if (joyy <= ZONE_Y_LOW && joyx >= ZONE_X_LOW && joyx <= ZONE_X_HIGH) {
|
|
|
72 |
l_dir = r_dir = MREVERSE;
|
|
|
73 |
r_spd = l_spd = map (joyy, MIN_Y, CENTER_Y, 254, 0);
|
|
|
74 |
|
|
|
75 |
// *2* Forward && dead zone of X
|
|
|
76 |
} else if (joyy >= ZONE_Y_HIGH && joyx >= ZONE_X_LOW && joyx <= ZONE_X_HIGH) {
|
|
|
77 |
l_dir = r_dir = MFORWARD;
|
|
|
78 |
r_spd = l_spd = map (joyy, CENTER_Y, MAX_Y, 0, 254);
|
|
|
79 |
|
|
|
80 |
// *3* Turn left && dead zone y
|
|
|
81 |
} else if (joyx <= ZONE_X_LOW && joyy >= ZONE_Y_LOW && joyy <= ZONE_Y_HIGH) {
|
|
|
82 |
l_dir = MREVERSE;
|
|
|
83 |
r_dir = MFORWARD;
|
|
|
84 |
r_spd = map (joyx, MIN_X, CENTER_X, 254, 0);
|
|
|
85 |
l_spd = r_spd * SKID_FACTOR;
|
|
|
86 |
|
|
|
87 |
// *4* Turn right && dead zone y
|
|
|
88 |
} else if (joyx >= ZONE_X_HIGH && joyy >= ZONE_Y_LOW && joyy <= ZONE_Y_HIGH) {
|
|
|
89 |
l_dir = MFORWARD;
|
|
|
90 |
r_dir = MREVERSE;
|
|
|
91 |
l_spd = map (joyx, CENTER_X, MAX_X, 0, 254);
|
|
|
92 |
r_spd = l_spd * SKID_FACTOR;
|
|
|
93 |
|
|
|
94 |
// *5* Forward turn left
|
|
|
95 |
} else if (joyx <= ZONE_X_LOW && joyy >= ZONE_Y_HIGH) {
|
|
|
96 |
// Max (jx:50,jy:205), Dis: 103
|
|
|
97 |
int dx = CENTER_X - joyx;
|
|
|
98 |
int dy = joyy - CENTER_Y;
|
|
|
99 |
int d = sqrt( (dx *dx) + (dy * dy) );
|
|
|
100 |
r_spd = map (d, 0, TURN_MAX, 0, 254);
|
|
|
101 |
l_spd = r_spd * TURN_FACTOR;
|
|
|
102 |
l_dir = r_dir = 1;
|
|
|
103 |
|
|
|
104 |
// *6* Forward turn right
|
|
|
105 |
} else if (joyx >= ZONE_X_HIGH && joyy >= ZONE_Y_HIGH) {
|
|
|
106 |
// Max (jx:200,jy:205), Dis: 103
|
|
|
107 |
int dx = joyx - CENTER_X;
|
|
|
108 |
int dy = joyy - CENTER_Y;
|
|
|
109 |
int d = sqrt( dx * dx + dy * dy);
|
|
|
110 |
l_spd = map (d, 0, TURN_MAX, 0, 254);
|
|
|
111 |
r_spd = l_spd * TURN_FACTOR;
|
|
|
112 |
l_dir = r_dir = 1;
|
|
|
113 |
|
|
|
114 |
// *7* Reverse turn right
|
|
|
115 |
} else if (joyx >= ZONE_X_HIGH && joyy <= ZONE_Y_LOW) {
|
|
|
116 |
int dx = joyx - CENTER_X;
|
|
|
117 |
int dy = CENTER_Y - joyy;
|
|
|
118 |
int d = sqrt( dx * dx + dy * dy);
|
|
|
119 |
l_spd = map (d, 0, TURN_MAX, 0, 254);
|
|
|
120 |
r_spd = l_spd * TURN_FACTOR;
|
|
|
121 |
l_dir = r_dir = 0;
|
|
|
122 |
|
|
|
123 |
// *8* Reverse turn left
|
|
|
124 |
} else if (joyx <= ZONE_X_LOW && joyy <= ZONE_Y_LOW) {
|
|
|
125 |
int dx = CENTER_X - joyx;
|
|
|
126 |
int dy = CENTER_Y - joyy;
|
|
|
127 |
int d = sqrt( dx * dx + dy * dy);
|
|
|
128 |
r_spd = map (d, 0, TURN_MAX, 0, 254);
|
|
|
129 |
l_spd = r_spd * TURN_FACTOR;
|
|
|
130 |
l_dir = r_dir = 0;
|
|
|
131 |
|
|
|
132 |
// In dead zone, don't move
|
|
|
133 |
} else {
|
|
|
134 |
l_dir = r_dir = MFORWARD;
|
|
|
135 |
r_spd = l_spd = 0;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
// Set the options based for track direction
|
|
|
139 |
if (r_dir == MFORWARD)
|
|
|
140 |
bitSet(options, 0);
|
|
|
141 |
if (l_dir == MFORWARD)
|
|
|
142 |
bitSet(options, 1);
|
|
|
143 |
|
|
|
144 |
// Prepare the send buffer
|
|
|
145 |
ttx_buffer[0] = options +1;
|
|
|
146 |
ttx_buffer[1] = r_spd + 1;
|
|
|
147 |
ttx_buffer[2] = l_spd + 1;
|
|
|
148 |
ttx_buffer[3] = 0x00;
|
|
|
149 |
|
|
|
150 |
Serial.print("opt: "); Serial.print((byte)options,DEC);
|
|
|
151 |
Serial.print("\trspd: "); Serial.print((byte)r_spd,DEC);
|
|
|
152 |
Serial.print("\tlspd: "); Serial.print((byte)l_spd,DEC);
|
|
|
153 |
Serial.println("");
|
|
|
154 |
|
|
|
155 |
digitalWrite(13, true); // Flash a light to show transmitting
|
|
|
156 |
vw_send((uint8_t *)ttx_buffer, strlen(ttx_buffer));
|
|
|
157 |
vw_wait_tx(); // Wait until the whole message is gone
|
|
|
158 |
digitalWrite(13, false);
|
|
|
159 |
}
|
|
|
160 |
loop_cnt++;
|
|
|
161 |
delay(1);
|
|
|
162 |
}
|
|
|
163 |
|