Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit b784e49

Browse files
Sprint 3 - Final
1 parent df1e1df commit b784e49

File tree

12 files changed

+593
-336
lines changed

12 files changed

+593
-336
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <String.h>
2+
3+
// variables
4+
const byte numChars = 32;
5+
char receivedChars[numChars];
6+
boolean newData = false;
7+
8+
void setup() {
9+
// put your setup code here, to run once:
10+
Serial.begin(9600);
11+
Serial.println("<Arduino is ready>");
12+
Serial.println(">");
13+
}
14+
15+
void loop() {
16+
// put your main code here, to run repeatedly:
17+
recvWithStartEndMarkers();
18+
showNewData();
19+
}
20+
21+
//////////////////////////////////////////////////////////////////////////////
22+
//////////////////////////////////////////////////////////////////////////////
23+
24+
void recvWithStartEndMarkers() {
25+
static boolean recvInProgress = false;
26+
static byte ndx = 0;
27+
char startMarker = '<';
28+
char endMarker = '>';
29+
char rc;
30+
31+
while (Serial.available() > 0 && newData == false) {
32+
rc = Serial.read();
33+
34+
if (recvInProgress == true) {
35+
if (rc != endMarker) {
36+
receivedChars[ndx] = rc;
37+
ndx++;
38+
if (ndx >= numChars) {
39+
ndx = numChars - 1;
40+
}
41+
}
42+
else {
43+
receivedChars[ndx] = '\0'; // terminate the string
44+
recvInProgress = false;
45+
ndx = 0;
46+
newData = true;
47+
}
48+
}
49+
50+
else if (rc == startMarker) {
51+
recvInProgress = true;
52+
}
53+
}
54+
}
55+
56+
//////////////////////////////////////////////////////////////////////////////
57+
//////////////////////////////////////////////////////////////////////////////
58+
59+
void showNewData() {
60+
if (newData == true) {
61+
//processData();
62+
Serial.print("Arduino: ");
63+
Serial.println(receivedChars);
64+
Serial.println(">");
65+
newData = false;
66+
}
67+
}

pi_contoller.py renamed to Utilities/Relay Code/pi_contoller.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def runTest(td):
9191
print ("")
9292

9393
serPort = "/dev/cu.SLAB_USBtoUART"
94+
#serPort = "/dev/ttyUSB0"
9495
baudRate = 9600
9596
ser = serial.Serial(serPort, baudRate)
9697
print ("Serial port " + serPort + " opened Baudrate " + str(baudRate))

Utilities/debounce.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/******************************************************************************
2+
debounce.c
3+
written by Kenneth A. Kuhn
4+
version 1.00
5+
6+
This is an algorithm that debounces or removes random or spurious
7+
transistions of a digital signal read as an input by a computer. This is
8+
particularly applicable when the input is from a mechanical contact. An
9+
integrator is used to perform a time hysterisis so that the signal must
10+
persistantly be in a logical state (0 or 1) in order for the output to change
11+
to that state. Random transitions of the input will not affect the output
12+
except in the rare case where statistical clustering is longer than the
13+
specified integration time.
14+
15+
The following example illustrates how this algorithm works. The sequence
16+
labeled, real signal, represents the real intended signal with no noise. The
17+
sequence labeled, corrupted, has significant random transitions added to the
18+
real signal. The sequence labled, integrator, represents the algorithm
19+
integrator which is constrained to be between 0 and 3. The sequence labeled,
20+
output, only makes a transition when the integrator reaches either 0 or 3.
21+
Note that the output signal lags the input signal by the integration time but
22+
is free of spurious transitions.
23+
24+
real signal 0000111111110000000111111100000000011111111110000000000111111100000
25+
corrupted 0100111011011001000011011010001001011100101111000100010111011100010
26+
integrator 0100123233233212100012123232101001012321212333210100010123233321010
27+
output 0000001111111111100000001111100000000111111111110000000001111111000
28+
29+
I have been using this algorithm for years and I show it here as a code
30+
fragment in C. The algorithm has been around for many years but does not seem
31+
to be widely known. Once in a rare while it is published in a tech note. It
32+
is notable that the algorithm uses integration as opposed to edge logic
33+
(differentiation). It is the integration that makes this algorithm so robust
34+
in the presence of noise.
35+
******************************************************************************/
36+
37+
/* The following parameters tune the algorithm to fit the particular
38+
application. The example numbers are for a case where a computer samples a
39+
mechanical contact 10 times a second and a half-second integration time is
40+
used to remove bounce. Note: DEBOUNCE_TIME is in seconds and SAMPLE_FREQUENCY
41+
is in Hertz */
42+
43+
#define DEBOUNCE_TIME 0.3
44+
#define SAMPLE_FREQUENCY 10
45+
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)
46+
47+
/* These are the variables used */
48+
unsigned int input; /* 0 or 1 depending on the input signal */
49+
unsigned int integrator; /* Will range from 0 to the specified MAXIMUM */
50+
unsigned int output; /* Cleaned-up version of the input signal */
51+
52+
53+
/* Step 1: Update the integrator based on the input signal. Note that the
54+
integrator follows the input, decreasing or increasing towards the limits as
55+
determined by the input state (0 or 1). */
56+
57+
if (input == 0)
58+
{
59+
if (integrator > 0)
60+
integrator--;
61+
}
62+
else if (integrator < MAXIMUM)
63+
integrator++;
64+
65+
/* Step 2: Update the output state based on the integrator. Note that the
66+
output will only change states if the integrator has reached a limit, either
67+
0 or MAXIMUM. */
68+
69+
if (integrator == 0)
70+
output = 0;
71+
else if (integrator >= MAXIMUM)
72+
{
73+
output = 1;
74+
integrator = MAXIMUM; /* defensive code if integrator got corrupted */
75+
}
76+
77+
/********************************************************* End of debounce.c */
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
#include <String.h>
2+
#include <Servo.h>
3+
4+
#define DEBOUNCE_TIME .5
5+
#define SAMPLE_FREQUENCY 20
6+
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)
7+
8+
// function declarations
9+
void RecvWithStartEndMarkers();
10+
void ProcessData();
11+
void ShowNewData();
12+
13+
// variables for communication
14+
const byte numChars = 32;
15+
char receivedChars[numChars];
16+
bool newData = false;
17+
18+
// variables for motor 1
19+
// ports
20+
Servo myservo1;
21+
byte motor_port = 3;
22+
byte motor_counter_port = 4;
23+
byte motor_reset_port = 2;
24+
// motor info
25+
String motor_direction = "";
26+
byte motor_rotation_number = 0;
27+
byte motor_sensor_counter1 = 0;
28+
byte motor_sensor_counter2 = 0;
29+
//
30+
// variables for motor 2
31+
Servo myservo2;
32+
byte motor_port2 = 9;
33+
byte motor_counter_port2 = 8;
34+
byte motor_reset_port2 = 7;
35+
// motor info
36+
String motor_direction2 = "";
37+
byte motor_rotation_number2 = 0;
38+
byte motor_sensor_counter12 = 0;
39+
byte motor_sensor_counter22 = 0;
40+
//
41+
byte input; /* 0 or 1 depending on the input signal */
42+
int integrator; /* Will range from 0 to the specified MAXIMUM */
43+
byte output; /* Cleaned-up version of the input signal */
44+
45+
byte input2; /* 0 or 1 depending on the input signal */
46+
int integrator2; /* Will range from 0 to the specified MAXIMUM */
47+
byte output2; /* Cleaned-up version of the input signal */
48+
49+
//////////////////////////////////////////////////////////////////////////////
50+
//////////////////////////////////////////////////////////////////////////////
51+
52+
void ProcessData() {
53+
54+
// variables
55+
String recievedString = "";
56+
String motor_rotation_string = "";
57+
String motor_rotation_string2 = "";
58+
// logic
59+
recievedString = receivedChars;
60+
motor_direction = getValue(recievedString, ',', 0);
61+
motor_rotation_string = getValue(recievedString, ',', 1);
62+
motor_rotation_number = motor_rotation_string.toInt();
63+
64+
//
65+
motor_direction2 = getValue(recievedString, ',', 2);
66+
motor_rotation_string2 = getValue(recievedString, ',', 3);
67+
motor_rotation_number2 = motor_rotation_string2.toInt();
68+
//
69+
70+
if (motor_direction == "Up") {
71+
myservo1.write(100);
72+
}
73+
else if (motor_direction == "Down") {
74+
myservo1.write(80);
75+
}
76+
77+
else if (motor_direction == "None"){
78+
myservo1.write(90);
79+
}
80+
else if (motor_direction == "Reset"){
81+
Reset1();
82+
}
83+
else
84+
{
85+
// TODO: make this exit the program
86+
Serial.println("<");
87+
Serial.print("Arduino: ");
88+
Serial.println("Invalid Input!");
89+
}
90+
91+
while(true) {
92+
93+
motor_sensor_counter2 = motor_sensor_counter1;
94+
checkswitch(motor_counter_port);
95+
motor_sensor_counter1 = output;
96+
delay(10);
97+
98+
if(motor_rotation_number == 0) {
99+
myservo1.write(90);
100+
break;
101+
}
102+
103+
if(digitalRead(motor_reset_port) == 0){
104+
myservo1.write(90);
105+
break;
106+
}
107+
108+
if(motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) {
109+
motor_rotation_number--;
110+
}
111+
}
112+
113+
if (motor_direction2 == "Up") {
114+
myservo2.write(100);
115+
}
116+
else if (motor_direction2 == "Down") {
117+
myservo2.write(80);
118+
}
119+
else if (motor_direction2 == "None"){
120+
myservo2.write(90);
121+
}
122+
else if (motor_direction2 == "Reset"){
123+
Reset2();
124+
}
125+
else
126+
{
127+
// TODO: make this exit the program
128+
Serial.println("<");
129+
Serial.print("Arduino: ");
130+
Serial.println("Invalid Input!");
131+
}
132+
133+
while(true) {
134+
135+
motor_sensor_counter22 = motor_sensor_counter12;
136+
checkswitch2(motor_counter_port2);
137+
motor_sensor_counter12 = output2;
138+
delay(10);
139+
140+
if(motor_rotation_number2 == 0) {
141+
myservo2.write(90);
142+
break;
143+
}
144+
145+
if(digitalRead(motor_reset_port2) == 0){
146+
myservo2.write(90);
147+
break;
148+
}
149+
150+
if(motor_sensor_counter12 == 1 && motor_sensor_counter22 == 0) {
151+
motor_rotation_number2--;
152+
}
153+
}
154+
155+
}
156+
157+
//////////////////////////////////////////////////////////////////////////////
158+
//////////////////////////////////////////////////////////////////////////////
159+
160+
void checkswitch(int switchPort) {
161+
/* Step 1: Update the integrator based on the input signal. Note that the
162+
integrator follows the input, decreasing or increasing towards the limits as
163+
determined by the input state (0 or 1). */
164+
input = digitalRead(switchPort);
165+
166+
if (input == 0)
167+
{
168+
if (integrator > 0)
169+
integrator--;
170+
}
171+
else if (integrator < MAXIMUM)
172+
integrator++;
173+
174+
/* Step 2: Update the output state based on the integrator. Note that the
175+
output will only change states if the integrator has reached a limit, either
176+
0 or MAXIMUM. */
177+
178+
if (integrator == 0)
179+
output = 0;
180+
else if (integrator >= MAXIMUM)
181+
{
182+
output = 1;
183+
integrator = MAXIMUM; /* defensive code if integrator got corrupted */
184+
}
185+
186+
/********************************************************* End of debounce.c */
187+
}
188+
189+
//////////////////////////////////////////////////////////////////////////////
190+
//////////////////////////////////////////////////////////////////////////////
191+
192+
void checkswitch2(int switchPort) {
193+
/* Step 1: Update the integrator based on the input signal. Note that the
194+
integrator follows the input, decreasing or increasing towards the limits as
195+
determined by the input state (0 or 1). */
196+
input2 = digitalRead(switchPort);
197+
198+
if (input2 == 0)
199+
{
200+
if (integrator2 > 0)
201+
integrator2--;
202+
}
203+
else if (integrator2 < MAXIMUM)
204+
integrator2++;
205+
206+
/* Step 2: Update the output state based on the integrator. Note that the
207+
output will only change states if the integrator has reached a limit, either
208+
0 or MAXIMUM. */
209+
210+
if (integrator2 == 0)
211+
output2 = 0;
212+
else if (integrator2 >= MAXIMUM)
213+
{
214+
output2 = 1;
215+
integrator2 = MAXIMUM; /* defensive code if integrator got corrupted */
216+
}
217+
218+
/********************************************************* End of debounce.c */
219+
}

0 commit comments

Comments
 (0)