Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed PinDetect
Fork of FinalMotorControl by
Diff: main.cpp
- Revision:
- 25:321b970eb3ff
- Parent:
- 24:24c91a6ae6ba
- Child:
- 26:57c62b925064
--- a/main.cpp Wed May 11 19:39:46 2016 +0000
+++ b/main.cpp Mon May 30 08:16:42 2016 +0000
@@ -3,7 +3,18 @@
#include "PinDetect.h"
Serial pc(USBTX, USBRX); // tx, rx
-PinDetect pb(A5);
+
+//Mid
+PinDetect pb3(A4);
+PinDetect pb4(A5);
+
+//Bottom
+PinDetect pb1(D14);
+PinDetect pb2(D15);
+
+//Top
+PinDetect pb5(D12);
+PinDetect pb6(D13);
//Analog Pins
//First engine
@@ -12,22 +23,26 @@
DigitalOut IN3(A2);
DigitalOut IN4(A3);
//Second engine
-DigitalOut IN5(D3);
-DigitalOut IN6(D4);
-DigitalOut IN7(D5);
-DigitalOut IN8(D6);
+DigitalOut IN5(D8);
+DigitalOut IN6(D9);
+DigitalOut IN7(D10);
+DigitalOut IN8(D11);
//Third Engine
-DigitalOut IN9(D8);
-DigitalOut IN10(D9);
-DigitalOut IN11(D10);
-DigitalOut IN12(D11);
+DigitalOut IN9(D3);
+DigitalOut IN10(D4);
+DigitalOut IN11(D5);
+DigitalOut IN12(D6);
-static int speed = 1500;
+static int speed = 2000;
+static int bootStep1 = 100;
+static int bootStep2 = 100;
+static int bootStep3 = 100;
static char m_cmd = 'x';
static bool e1 = true;
static bool e2 = true;
static bool e3 = true;
+static bool rest = false;
static int steps1 = 0;
static int steps2 = 0;
@@ -410,149 +425,226 @@
}
}
-//SPECIFY WHICH BUTTON IS PRESSED. NEED ONE CALLBACK FOR EACH BUTTON.
-void pb_hit_callback (void)
+
+//MOTOR 1 LEFT SIDE BUTTON
+void pb1_hit_callback (void)
{
- e1 = false;
- //m_cmd = 'x';
- /*
+ if(rest == false)
+ {
+ e1 = false;
+
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine1Right();
+ stepsToCenter --;
+ }
+ //break;
+ //end if
+ }
+
- */
+}
+
+
+//MOTOR 1 RIGHT SIDE BUTTON
+void pb2_hit_callback (void)
+{
+ if(rest == false)
+ {
+ e1 = false;
+ //m_cmd = 'x';
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine1Left();
+ stepsToCenter --;
+ }
+ }
+
+}
+//MOTOR 2 LEFT SIDE BUTTON
+void pb3_hit_callback (void)
+{
+ if(rest == false)
+ {
+ e2 = false;
+ //m_cmd = 'x';
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine2Left();
+ stepsToCenter --;
+ }
+ }
}
+//MOTOR 2 RIGHT SIDE BUTTON
+void pb4_hit_callback (void)
+{
+ if(rest == false)
+ {
+ e2 = false;
+ //m_cmd = 'x';
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine2Right();
+ stepsToCenter --;
+ }
+ }
+
+
+}
+
+//MOTOR 3 LEFT SIDE BUTTON
+void pb5_hit_callback (void)
+{
+ if(rest == false)
+ {
+ e3 = false;
+ //m_cmd = 'x';
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine3Left();
+ stepsToCenter --;
+ }
+ }
+
+
+}
+//MOTOR 3 RIGHT SIDE BUTTON
+void pb6_hit_callback (void)
+{
+ if(rest == false)
+ {
+ e3 = false;
+ //m_cmd = 'x';
+ speed = 2000;
+ Thread::wait(1000);
+ int stepsToCenter = 242;
+
+ while (stepsToCenter >= 0)
+ {
+ stepEngine3Right();
+ stepsToCenter --;
+ }
+ }
+}
int main()
{
- pb.mode(PullUp);
- pb.attach_deasserted(&pb_hit_callback);
- pb.setSampleFrequency();
+ pb1.mode(PullUp);
+ pb2.mode(PullUp);
+ pb3.mode(PullUp);
+ pb4.mode(PullUp);
+ pb5.mode(PullUp);
+ pb6.mode(PullUp);
+
+ //Set up buttons 1 and 2
+ pb1.attach_deasserted(&pb1_hit_callback);
+ pb1.setSampleFrequency();
+ pb2.attach_deasserted(&pb2_hit_callback);
+ pb2.setSampleFrequency();
//Thread 1 has constant feed from usb
Thread t1(input);
//static char global_direction;
printf("Started");
-
-
- /*
-
- BOOT UP AND SYNCHRONIZE
- while (true)
- {
- should move one engine at a time. If not, alot of work has to be done in if/else statements as well as new engine methods..
- while(moving bottom part 180 degrees to one side / steps < 0)
- {
- step bottom engine in one direction.
- callback will fire and stop the engine
- }
- while(moving middle part 180 degrees to one side / steps < 0)
- {
- step middle engine in one direction
- callback fires and stops the engine
- }
- while(moving middle part 180 degrees to one side / steps < 0)
- {
- step middle engine in one direction
- callback fires and stops the engine
- }
- //Break the while loop (FALSE)
- break;
-
-
- }
-
- */
// Move motor in one direction
while (true)
{
speed = 2000;
+
+ while(bootStep1 >= 0)
+ {
+ bootStep1--;
+ stepEngine1Right();
+ }
stepEngine1Left();
if (e1 == false)
{
Thread::wait(1000);
- int stepsToCenter = 242;
-
- while (stepsToCenter >= 0)
- {
- stepEngine1Right();
- stepsToCenter --;
- }
- break;
+ break;
}//end if
-
+
}//end while
- /*
+
+ //Set up buttons 3 and 4
+ pb3.attach_deasserted(&pb3_hit_callback);
+ pb3.setSampleFrequency();
+ pb4.attach_deasserted(&pb4_hit_callback);
+ pb4.setSampleFrequency();
+
while (true)
{
- stepEngine2Right();
+
+ speed = 2000;
+ while(bootStep2 >= 0)
+ {
+ bootStep2--;
+ stepEngine2Right();
+
+ }
+
+ stepEngine2Left();
if (e2 == false)
{
- Thread::wait(1000);
- int stepsToCenter = 242;
+ pc.printf("HALLO");
- while (stepsToCenter >= 0)
- {
- stepEngine2Left();
- stepsToCenter --;
- }
+ Thread::wait(1000);
break;
}//end if
}
+
+ pb5.attach_deasserted(&pb5_hit_callback);
+ pb5.setSampleFrequency();
+ pb6.attach_deasserted(&pb6_hit_callback);
+ pb6.setSampleFrequency();
while (true)
{
- stepEngine2Left();
+ speed = 2000;
+ while(bootStep3 >= 0)
+ {
+ bootStep3--;
+ stepEngine3Right();
+
+ }
+ stepEngine3Left();
if (e3 == false)
{
+ pc.printf("HALLO");
Thread::wait(1000);
- int stepsToCenter = 242;
-
- while (stepsToCenter >= 0)
- {
- stepEngine3Right();
- stepsToCenter --;
- }
break;
}//end if
}
- */
-
while (true)
{
speed = 1500;
- if(m_cmd == 'a')
- {
- stepAllRight();
- }
-
- else if (m_cmd == 'b')
- {
- stepAllLeft();
-
- }
-
- /*
- else if (m_cmd == 'k')
- {
- int steps = 242;
-
-
- //Steps = 242 gives a 60' rotation to the left.
- while (steps >= 0)
- {
- stepAllLeft();
- steps--;
- }
- m_cmd = 'x';
- } */
-
-
//Release all motors / Set all pins to 0
if (m_cmd == 'z')
{
@@ -562,79 +654,80 @@
if (m_cmd == 'x')
{
}
+
+ //Rest mode. Make the engines ignore pushbuttons.
+ /*
+ if(m_cmd == 'r')
+ {
+ rest = true;
+ //Step motors to rest position
+
+
+ }
+ */
//Controlling each motor seperately.
- //ENGINE 1
+ //ENGINE 3
if(m_cmd == '7' && steps3 > -242)
{
+ rest = false;
steps3 --;
stepEngine3Left();
}
else if (m_cmd == '9' && steps3 < 242)
{
+ rest = false;
stepEngine3Right();
steps3 ++;
}
//ENGINE 2
else if (m_cmd == '4' && steps2 > -242)
{
+ rest = false;
steps2 --;
stepEngine2Left();
}
else if (m_cmd == '6' && steps2 < 242)
{
+ rest = false;
steps2 ++;
stepEngine2Right();
}
//ENGINE 3
else if (m_cmd == '1' && steps1 > -242)
{
+ rest = false;
steps1--;
stepEngine1Left();
}
else if (m_cmd == '3' && steps1 < 242)
{
+ rest = false;
steps1++;
stepEngine1Right();
}
- else if (m_cmd == 'y')
- {
- int a = 121;
- int b = 60;
- int c = 60;
-
- while (a >= 0)
- {
- stepEngine1Left();
- a--;
- }
- while (b >= 0)
- {
- stepEngine2Right();
- b--;
- }
- while (c >= 0)
- {
- stepEngine3Left();
- c--;
- }
-
- m_cmd = 'x';
+
+
- }
- /*
- new_pb = pb;
- if (new_pb == '1')
- {
- m_cmd ='x';
- }
- */
- //Thread::wait(100);
- //pc.printf("%d", steps3);
+ //Thread::wait(10);
+ //pc.printf("%d", steps2);
} //END WHILE TRUE
} //END Main
+
+
+
+
+
+
+
+
+
+
+
+
+
