Skip to content

Commit 13e63ee

Browse files
committed
working code for new arduino based sorter
Thank you M for teaching me how to compare colors
1 parent 9e6d8a7 commit 13e63ee

4 files changed

Lines changed: 302 additions & 395 deletions

File tree

arduino/sort_test/ColorMath.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "ColorMath.h"
2+
#include "Arduino.h"
3+
ColorMath::ColorMath() {
4+
5+
}
6+
int ColorMath::get_raw_diff(uint16_t tgt_read_a[], uint16_t tgt_read_b[]) {
7+
int sum=0;
8+
for(int i=0;i<10;i++) {
9+
sum+=abs(tgt_read_a[i]-tgt_read_b[i]);
10+
}
11+
return int(sum/8);
12+
}
13+
double ColorMath::get_trig_diff(uint16_t tgt_read_a[], uint16_t tgt_read_b[]) {
14+
double sum = 0;
15+
for(int i=0;i<8;i++) {
16+
sum+= sq(tgt_read_a[i]-tgt_read_b[i]);
17+
}
18+
return sqrt(sum);
19+
}
20+
double ColorMath::get_cos_dist(uint16_t tgt_read_a[], uint16_t tgt_read_b[]) { //Thank you M for teaching me how this works
21+
return abs(1.00 - (pairwise_product(tgt_read_a,tgt_read_b) / (get_mag(tgt_read_a)*get_mag(tgt_read_b))));
22+
23+
24+
// return pairwise_product(tgt_read_a,tgt_read_b) / (get_mag(tgt_read_a)*get_mag(tgt_read_b));
25+
}
26+
27+
double ColorMath::get_mag(uint16_t tgt_read_a[]) {
28+
double sum = 0;
29+
for(int i=0; i<8; i++ ) {
30+
sum+= tgt_read_a[i] * tgt_read_a[i];//sq(tgt_read_a[i]);
31+
}
32+
return sqrt(sum);
33+
}
34+
double ColorMath::pairwise_product(uint16_t tgt_read_a[],uint16_t tgt_read_b[]){
35+
double sum = 0;
36+
for(int i=0;i<8;i++) {
37+
sum += tgt_read_a[i] * tgt_read_b[i];
38+
}
39+
return sum;
40+
}

arduino/sort_test/ColorMath.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef ColorMath_h
2+
#define ColorMath_h
3+
#include "Arduino.h"
4+
class ColorMath
5+
{
6+
public:
7+
ColorMath();
8+
int get_raw_diff(uint16_t tgt_read_a[], uint16_t tgt_read_b[]);
9+
double get_cos_dist(uint16_t tgt_read_a[], uint16_t tgt_read_b[]);
10+
double get_trig_diff(uint16_t tgt_read_a[], uint16_t tgt_read_b[]);
11+
private:
12+
bool _debug;
13+
double get_mag(uint16_t tgt_read_a[]);
14+
double pairwise_product(uint16_t tgt_read_a[],uint16_t tgt_read_b[]);
15+
};
16+
#endif

arduino/sort_test/sort_test.ino

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#include <Adafruit_AS7341.h>
2+
#include <Stepper.h>
3+
#include <Servo.h>
4+
#include "ColorMath.h"
5+
6+
7+
#define SERVO_PIN A2
8+
#define SERVO_CYCLE 200
9+
bool agg_state = true;
10+
Servo agg_servo;
11+
12+
Adafruit_AS7341 as7341;
13+
14+
#define SENSOR_SIZE 8
15+
uint16_t blank_readings[SENSOR_SIZE] = {1,1,1,1,1,1,1,1};
16+
uint16_t tgt_readings[SENSOR_SIZE] = {6,61,52,72,65,54,46,26};
17+
uint16_t max_reading[SENSOR_SIZE] = {0,0,0,0,0,0,0,0};
18+
double max_read = 0;
19+
double avg_read = 0.0;
20+
21+
bool sort_mode = false;
22+
unsigned long myTime = 0;
23+
//joystick
24+
25+
#define VRX_PIN A0 // Arduino pin connected to VRX pin
26+
#define VRY_PIN A1 // Arduino pin connected to VRY pin
27+
#define SW_PIN 2
28+
// ezButton button(SW_PIN);
29+
#define SORT_MODE_PIN 4
30+
#define WIGGLE_RANGE 10
31+
#define GRAB_RANGE 30
32+
int x_val = 0; // To store value of the X axis
33+
int y_val = 0; // To store value of the Y axis
34+
#define JOYSTICK_CAL_CNT 20
35+
int button_val = 0; // To store value of the button
36+
37+
#define LED_CURRENT 10
38+
#define ATIME_VAL 99
39+
#define ASTEP_VAL 499
40+
41+
42+
#define ENDSTOP_PIN 3
43+
int endstop_val = 0;
44+
#define _RANGE 10
45+
#define CENTER_POS 90
46+
47+
//stepper
48+
#define STEPS_PER_REV 2038
49+
#define CHAMBER_SPEED 6
50+
#define EXHUAST_SPEED 10
51+
int step_dir=0;
52+
Stepper ch_stepper = Stepper(STEPS_PER_REV, 13,11,12,10); //chamber stepper
53+
Stepper ex_stepper = Stepper(STEPS_PER_REV, 8,6,7,5); //exhaust stepper
54+
55+
#define GAP_TO_EXHAUST 200
56+
57+
bool found_buffer = false;
58+
#define PEAK_BUFFER_SIZE 10
59+
double max_readings[PEAK_BUFFER_SIZE];
60+
uint16_t cur_read_array[PEAK_BUFFER_SIZE][SENSOR_SIZE];
61+
ColorMath cm;
62+
void setup() {
63+
// put your setup code here, to run once:
64+
Serial.begin(115200);
65+
// Wait for communication with the host computer serial monitor
66+
while (!Serial) {
67+
delay(1);
68+
}
69+
Serial.println("code Starting");
70+
if (!as7341.begin()){
71+
Serial.println("code Could not find AS7341");
72+
while (1) { delay(10); }
73+
}
74+
//initializer buffer
75+
76+
Serial.println("code\tBUFFER STUFF");
77+
for(int i=0;i<PEAK_BUFFER_SIZE;i++) {
78+
for(int j=0;j<SENSOR_SIZE;j++) {
79+
cur_read_array[i][j] = 1;
80+
}
81+
}
82+
reset_max_array();
83+
pinMode(ENDSTOP_PIN,INPUT_PULLUP);
84+
pinMode(SW_PIN, INPUT_PULLUP);
85+
pinMode(SORT_MODE_PIN,INPUT_PULLUP);
86+
87+
// SETUP READER
88+
as7341.enableLED(true);
89+
as7341.setATIME(ATIME_VAL);
90+
as7341.setASTEP(ASTEP_VAL);
91+
as7341.enableSpectralInterrupt(false);
92+
as7341.setLEDCurrent(LED_CURRENT);
93+
as7341.setGain(AS7341_GAIN_1X);
94+
ch_stepper.setSpeed(CHAMBER_SPEED);
95+
ex_stepper.setSpeed(EXHUAST_SPEED);
96+
97+
//Servo initialization
98+
Serial.println("code\tWHATWHAT");
99+
agg_servo.attach(SERVO_PIN);
100+
agg_servo.write(90);
101+
reset_exhaust();
102+
step_dir = -10;
103+
//manual
104+
Serial.println("code\tstarting intial reads");
105+
for(int i=0;i<PEAK_BUFFER_SIZE;i++) {
106+
read_cur_color();
107+
ch_stepper.step(-1);
108+
}
109+
Serial.println("code\tSETUP COMPLETE");
110+
}
111+
void reset_max_array(){
112+
for(int i=0;i<PEAK_BUFFER_SIZE;i++) {
113+
max_readings[i]=999-i;
114+
}
115+
for(int i=0;i<SENSOR_SIZE;i++) {
116+
max_reading[i]=1;
117+
}
118+
Serial.println("code\treset max array compelted");
119+
}
120+
void reset_exhaust() {
121+
endstop_val = digitalRead(ENDSTOP_PIN);
122+
while(endstop_val!=0) {
123+
ex_stepper.step(1);
124+
endstop_val = digitalRead(ENDSTOP_PIN);
125+
}
126+
Serial.println("code exhaust reset complete");
127+
delay(100);
128+
}
129+
void set_max_reading(uint16_t tgt_read_a[]) {
130+
for(int i=0;i<SENSOR_SIZE;i++) {
131+
if(tgt_read_a[i]<max_reading[i]) {
132+
return;
133+
}
134+
}
135+
136+
for(int i=0;i<SENSOR_SIZE;i++) {
137+
max_reading[i]= tgt_read_a[i];
138+
139+
}
140+
}
141+
void print_max_color() {
142+
Serial.print("maxx");
143+
for(int i=0;i<SENSOR_SIZE;i++) {
144+
Serial.print(max_reading[i]);
145+
Serial.print("\t");
146+
}
147+
Serial.println("");
148+
}
149+
void read_cur_color() {
150+
uint16_t cur_reading[SENSOR_SIZE] ={1,1,1,1,1,1,1,1};
151+
as7341.readAllChannels();
152+
cur_reading[0] = as7341.getChannel(AS7341_CHANNEL_415nm_F1);
153+
cur_reading[1] = as7341.getChannel(AS7341_CHANNEL_445nm_F2);
154+
cur_reading[2] = as7341.getChannel(AS7341_CHANNEL_480nm_F3);
155+
cur_reading[3] = as7341.getChannel(AS7341_CHANNEL_515nm_F4);
156+
cur_reading[4] = as7341.getChannel(AS7341_CHANNEL_555nm_F5);
157+
cur_reading[5] = as7341.getChannel(AS7341_CHANNEL_590nm_F6);
158+
cur_reading[6] = as7341.getChannel(AS7341_CHANNEL_630nm_F7);
159+
cur_reading[7] = as7341.getChannel(AS7341_CHANNEL_680nm_F8);
160+
for(int i=PEAK_BUFFER_SIZE-1;i>0;i--) {
161+
max_readings[i] = max_readings[i-1]; //shift max readings
162+
for(int j=0;j<SENSOR_SIZE;j++) {
163+
cur_read_array[i][j] = cur_read_array[i-1][j];
164+
}
165+
}
166+
for(int i=0;i<SENSOR_SIZE;i++) {
167+
cur_read_array[0][i] = cur_reading[i];
168+
Serial.print(cur_read_array[0][i]);
169+
Serial.print("\t");
170+
}
171+
set_max_reading(cur_reading);
172+
double cur_max = cm.get_trig_diff(cur_reading,blank_readings);
173+
max_readings[0]=cur_max;
174+
Serial.print(max_readings[0]);
175+
176+
double cur_diff = cm.get_cos_dist(tgt_readings,cur_reading);
177+
Serial.print("\t");
178+
Serial.print(cur_diff,6);
179+
double cur_max_diff = cm.get_cos_dist(tgt_readings,max_reading);
180+
Serial.print("\t");
181+
Serial.println(cur_max_diff,6);
182+
// step_dir = map_ch_speed(max_readings[0]);
183+
print_max_color();
184+
}
185+
bool ended_peak() {
186+
//do stuff to detect the peak
187+
for(int i=0;i<PEAK_BUFFER_SIZE-1;i++) {
188+
if(max_readings[i]>max_readings[i+1]) {
189+
return false;
190+
}
191+
}
192+
return true;
193+
}
194+
int map_ch_speed(int tgt_reading){
195+
return map(min(tgt_reading,120),10,120,-30,-2);
196+
}
197+
void save_tgt_color() {
198+
Serial.println("code 0,0,0,0,0,0,0,0,0,0,0");
199+
as7341.readAllChannels();
200+
}
201+
202+
void loop() {
203+
// put your main code here, to run repeatedly:
204+
x_val = analogRead(VRX_PIN);
205+
y_val = analogRead(VRY_PIN);
206+
207+
button_val = digitalRead(SW_PIN);
208+
endstop_val = digitalRead(ENDSTOP_PIN);
209+
sort_mode = digitalRead(SORT_MODE_PIN);
210+
211+
ch_stepper.step(step_dir);
212+
213+
//if sort_mode enabled
214+
read_cur_color();
215+
if(ended_peak()) {
216+
double cur_diff = cm.get_cos_dist(tgt_readings,max_reading);
217+
Serial.print("code\twe found a peak ");
218+
Serial.println(cur_diff,6);
219+
if(sort_mode) {
220+
reset_exhaust();
221+
if(cur_diff<0.002){
222+
Serial.print("code\tmatch found ");
223+
Serial.println(cur_diff,6);
224+
ex_stepper.step(-STEPS_PER_REV*0.2);
225+
226+
} else {
227+
}
228+
ch_stepper.step(-STEPS_PER_REV/4);
229+
ch_stepper.step(int(STEPS_PER_REV*0.1));
230+
reset_exhaust();
231+
}
232+
reset_max_array();
233+
234+
}
235+
myTime+=1;
236+
237+
if(myTime < SERVO_CYCLE) {
238+
agg_servo.write(90);
239+
}
240+
if(myTime >SERVO_CYCLE && myTime < SERVO_CYCLE+50){
241+
agg_servo.write(96);
242+
}
243+
if(myTime>SERVO_CYCLE+50) {
244+
myTime = 0;
245+
}
246+
}

0 commit comments

Comments
 (0)