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

Commit 41c1983

Browse files
Finished adding basic dynamic motor control code
Doesn't include reset functionality
1 parent b8a6e1c commit 41c1983

File tree

2 files changed

+64
-69
lines changed

2 files changed

+64
-69
lines changed

arduino_controller/arduino_controller.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void RecvWithStartEndMarkers();
1212
void ProcessData();
1313
void CheckSwitch();
1414
void PopulateArray();
15+
void CheckSwitch();
1516

1617
// variables for communication
1718
const byte num_chars = 32;
@@ -77,6 +78,13 @@ void setup()
7778
Serial.print(" ");
7879
pinMode(x, INPUT_PULLUP);
7980
}
81+
82+
// zero all motors
83+
for (int i = 0; i < NUMBER_MOTORS; i++)
84+
{
85+
my_servo[i].write(90);
86+
}
87+
8088
Serial.println("");
8189
Serial.println("<");
8290
Serial.println("Arduino is ready");

arduino_controller/arduino_controller_functions.ino

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ void ProcessData()
1515
// initialize all motors and get them moving
1616
for (int i = 0; i < NUMBER_MOTORS; i++)
1717
{
18-
if (motor_commands[i][1] == 0)
18+
if (motor_commands[i][0] == 0)
1919
{
2020
my_servo[i].write(100);
2121
}
22-
else if (motor_commands[i][1] == 1)
22+
else if (motor_commands[i][0] == 1)
2323
{
2424
my_servo[i].write(80);
2525
}
26-
else if (motor_commands[i][1] == 2)
26+
else if (motor_commands[i][0] == 2)
2727
{
2828
my_servo[i].write(90);
2929
}
30-
else if (motor_commands[i][1] == 3)
30+
else if (motor_commands[i][0] == 3)
3131
{
3232
// call reset function (still needs to be written)
3333
}
@@ -37,42 +37,41 @@ void ProcessData()
3737
Invalid();
3838
}
3939
}
40+
41+
bool go = true;
42+
int total_turns = 0;
4043

44+
while (go == true)
45+
{
4146

47+
for (int i = 0; i < NUMBER_MOTORS; i++)
48+
{
49+
motor_sensor_counter2[i] = motor_sensor_counter1[i];
50+
motor_sensor_counter1 = CheckSwitch(i,ports[i][0]);
4251

43-
//
44-
/*
45-
// Print First Number
46-
Serial.println("---------");
47-
Serial.print("Motor 1: ");
48-
Serial.println(motor_rotation_number);
49-
50-
while (true) {
51-
52-
motor_sensor_counter2 = motor_sensor_counter1;
53-
checkswitch(motor_counter_port);
54-
motor_sensor_counter1 = output;
55-
delay(10);
52+
if(motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) {
53+
motor_commands[i][1] = motor_commands[i][1] - 1;
54+
}
55+
}
5656

57-
if (motor_rotation_number == 0) {
58-
myservo1.write(90);
59-
break;
57+
// stop motors that have reached 0
58+
for (int i = 0; i < NUMBER_MOTORS; i++)
59+
{
60+
if (motor_commands[i][1] <= 0) {
61+
my_servo[i].write(90);
62+
}
6063
}
61-
62-
if (digitalRead(motor_reset_port) == 0) {
63-
myservo1.write(90);
64-
break;
64+
// see how many turns are left in the array
65+
for (int i = 0; i < NUMBER_MOTORS; i++)
66+
{
67+
total_turns += motor_commands[i][1];
6568
}
66-
67-
if (motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) {
68-
motor_rotation_number--;
69-
70-
// Print Number Every Time It Changes
71-
Serial.print("Motor 1: ");
72-
Serial.println(motor_rotation_number);
69+
// exit loop if there are no more motor rotations remaining
70+
if (total_turns <= 0) {
71+
go = false;
7372
}
74-
} */
75-
73+
total_turns = 0;
74+
}
7675
// Send Finished Signal
7776
Finished();
7877
}
@@ -139,42 +138,30 @@ void PopulateArray()
139138
//////////////////////////////////////////////////////////////////////////////
140139
//////////////////////////////////////////////////////////////////////////////
141140

142-
void CheckSwitch(int switchPort)
141+
void CheckSwitch(int motor_number, int switchPort)
143142
{
144-
// /* Step 1: Update the integrator based on the input signal. Note that the
145-
// integrator follows the input, decreasing or increasing towards the limits
146-
// as determined by the input state (0 or 1). */
147-
// input = digitalRead(switchPort);
148-
149-
// if (input == 0)
150-
// {
151-
// if (integrator > 0)
152-
// integrator--;
153-
// }
154-
// else if (integrator < MAXIMUM)
155-
// integrator++;
156-
157-
// /* Step 2: Update the output state based on the integrator. Note that the
158-
// output will only change states if the integrator has reached a limit,
159-
// either 0 or MAXIMUM. */
160-
161-
// if (integrator == 0)
162-
// output = 0;
163-
// else if (integrator >= MAXIMUM)
164-
// {
165-
// output = 1;
166-
// integrator = MAXIMUM; /* defensive code if integrator got corrupted */
167-
// }
168-
169-
// /********************************************************* End of
170-
// debounce.c */
171-
}
143+
/* Step 1: Update the integrator based on the input signal. Note that the
144+
integrator follows the input, decreasing or increasing towards the limits
145+
as determined by the input state (0 or 1). */
146+
input[motor_number] = digitalRead(switchPort);
172147

173-
/* void Reset1() {
174-
myservo1.write(100);
175-
while(true) {
176-
if(digitalRead(motor_reset_port) == 0){
177-
break;
178-
}
148+
if (input[motor_number] == 0)
149+
{
150+
if (integrator[motor_number] > 0)
151+
integrator[motor_number]--;
152+
}
153+
else if (integrator[motor_number] < MAXIMUM)
154+
integrator[motor_number]++;
155+
156+
/* Step 2: Update the output state based on the integrator. Note that the
157+
output will only change states if the integrator has reached a limit,
158+
either 0 or MAXIMUM. */
159+
160+
if (integrator[motor_number] == 0)
161+
reuturn(0)
162+
else if (integrator[motor_number] >= MAXIMUM)
163+
{
164+
return(1);
165+
integrator[motor_number] = MAXIMUM; /* defensive code if integrator got corrupted */
179166
}
180-
} */
167+
}

0 commit comments

Comments
 (0)