-
Notifications
You must be signed in to change notification settings - Fork 1
/
swingLight4.ino
177 lines (156 loc) · 3.94 KB
/
swingLight4.ino
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
#include <FreeSixIMU.h> // library for IMU
#include <FIMU_ADXL345.h>
#include <FIMU_ITG3200.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <Swing.h>
#include <stdint.h>
#include <math.h>
// #include <LightingModel.h>
// #include "globals.h"
float SWING_LENGTH = 3.3;
FreeSixIMU SIXDOF = FreeSixIMU();
float PITCH_ROLL_OFFSET = 75.0;
Swing SWING = Swing(SWING_LENGTH);
const int PIXELS_PER_STRIP=240;
#define PIN 6
Adafruit_NeoPixel STRIP = Adafruit_NeoPixel(PIXELS_PER_STRIP, PIN, NEO_GRB + NEO_KHZ800);
bool DEBUG=true;
// #include <HSV.h>
// #include <Diameter.h>
// timing constants
int ANG_TIME_DELAY=20; // milliseconds
int ANG_LAST_TIME;
int LED_TIME_DELAY=20; // milliseconds
int LED_LAST_TIME;
typedef struct RgbColor {
unsigned char r;
unsigned char g;
unsigned char b;
}RgbColor;
typedef struct HsvColor{
unsigned char h;
unsigned char s;
unsigned char v;
}HsvColor;
class HSV { //: public LightingModel {
public:
HSV(){};
HsvColor hsvStrip[240];
void update(){
STRIP.setPixelColor(120, STRIP.Color(0,0,255));
STRIP.show();
}
void setup(){
for (uint8_t i = 0; i < 240; i++){
hsvStrip[i].h = 0;
hsvStrip[i].s = 0;
hsvStrip[i].v = 0;
}
}
RgbColor HsvToRgb(HsvColor hsv){
RgbColor rgb;
unsigned char region, remainder, p, q, t;
if (hsv.s == 0)
{
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
rgb.r = hsv.v; rgb.g = t; rgb.b = p;
break;
case 1:
rgb.r = q; rgb.g = hsv.v; rgb.b = p;
break;
case 2:
rgb.r = p; rgb.g = hsv.v; rgb.b = t;
break;
case 3:
rgb.r = p; rgb.g = q; rgb.b = hsv.v;
break;
case 4:
rgb.r = t; rgb.g = p; rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v; rgb.g = p; rgb.b = q;
break;
}
return rgb;
};
//HsvColor RgbToHsv(RgbColor rgb);
void setStrip(){
for (uint8_t i = 0; i<240; i++){
STRIP.setPixelColor(i, STRIP.Color(255,0,0));
}
// RgbColor rgb;
// for (uint8_t i = 0; i < 240; i++){
// rgb = HsvToRgb(hsvStrip[i]);
// STRIP.setPixelColor(i, STRIP.Color(rgb.r, rgb.g, rgb.b));
// }
};
};
void setup() {
setupIMU();
setupStrip();
}
float ypr[3]; // yaw pitch roll
HSV *hsv = new HSV();
void setStripColor(int i, RgbColor rgb) {
STRIP.setPixelColor(i, STRIP.Color(rgb.r, rgb.g, rgb.b));
}
int counter = 0;
void loop() {
updateAngle();
counter = counter + 1;
int theta = (int) (SWING.getTheta() + 90);
// Serial.println(theta);
HsvColor hsvcolor;
int base = counter;
for (int i = 0; i < 240; i++) {
hsvcolor.v = 255;
hsvcolor.s = 255;
hsvcolor.h = ((base + i) * 255/239)%256;
RgbColor rgb = hsv->HsvToRgb(hsvcolor);
if (hsvcolor.h > 120 && hsvcolor.h < 150) {
rgb.r = abs(hsvcolor.h - 135);
rgb.g = abs(hsvcolor.h - 135) * 5;
rgb.b = abs(hsvcolor.h - 135) * 5;
}
setStripColor(i, rgb);
}
// theta = (theta + 180) % 360;
// for (int i = 120; i < 240; i++) {
// hsvcolor.v = 255;
// hsvcolor.s = i * i / 255;
// hsvcolor.h = counter;
// RgbColor rgb = hsv->HsvToRgb(hsvcolor);
// STRIP.setPixelColor(i, STRIP.Color(rgb.r, rgb.g, rgb.b));
// }
STRIP.show();
}
void updateAngle(){
float angle;
SIXDOF.getYawPitchRoll(ypr);
angle = ypr[1] * sin(PITCH_ROLL_OFFSET*M_PI/180) + ypr[2] * cos(PITCH_ROLL_OFFSET*M_PI/180);
SWING.updatePhysics(angle);
}
void setupIMU() {
Serial.begin(9600);
Wire.begin();
delay(5);
SIXDOF.init(); //begin the IMU
delay(5);
}
void setupStrip() {
STRIP.begin();
STRIP.show();
}