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 mbed-rtos TextLCD
Revision 26:83550fc299aa, committed 2019-02-19
- Comitter:
- thomasmorris
- Date:
- Tue Feb 19 14:40:01 2019 +0000
- Parent:
- 25:9751619fa030
- Commit message:
- Working code (needs stepper motor and mosfet output checking
Changed in this revision
--- a/Interface/Interface.cpp Mon Feb 18 22:04:54 2019 +0000
+++ b/Interface/Interface.cpp Tue Feb 19 14:40:01 2019 +0000
@@ -1,9 +1,7 @@
#include "Interface.hpp" //Include the header file, this acts like a series of forward declarations
//Constructor
INTERFACE::INTERFACE(){}//Set all values to 0
-
INTERFACE::~INTERFACE(){} //Destructor
-//Interrupt run functions
void INTERFACE::Interface_Init()//Set all values to 0 bar the default turns
{
Led_Select_Left_mutex.lock();
@@ -15,7 +13,7 @@
Led_Select_Right_mutex.unlock();
_Function_mutex.lock();
- _Function =2;
+ _Function = Type_of_Rig;
_Function_mutex.unlock();
_Twist_Go_mutex.lock();
@@ -38,17 +36,6 @@
_Turns_Done =0;
_Turns_Done_mutex.unlock();
- _Turns_Todo_mutex.lock();
- if(Type_of_Rig == 2)
- {
- _Turns_Todo = Default_Coiling_Turns;
- }
- else if(Type_of_Rig == 3)
- {
- _Turns_Todo = Default_Twisting_Turns;
- }
- _Turns_Todo_mutex.unlock();
-
_Loop_mutex.lock();
_Loop =6;
_Loop_mutex.unlock();
@@ -86,50 +73,69 @@
_System_Running_mutex.unlock();
- Led_Select_Left_mutex.lock();
- Led_Select_Left = 0;
- Led_Select_Left_mutex.unlock();
+ Led_Select_Left_mutex.lock();
+ Led_Select_Left = 0;
+ Led_Select_Left_mutex.unlock();
- Led_Select_Right_mutex.lock();
- Led_Select_Right= 1;
- Led_Select_Right_mutex.unlock();
+ Led_Select_Right_mutex.lock();
+ Led_Select_Right= 1;
+ Led_Select_Right_mutex.unlock();
+
+ _Turns_Todo_mutex.lock();
+ if(Type_of_Rig == 2)
+ {
+ _Turns_Todo = Default_Coiling_Turns;
+ }
+ else if(Type_of_Rig == 3)
+ {
+ _Turns_Todo = Default_Twisting_Turns;
+ }
+ _Turns_Todo_mutex.unlock();
}
void INTERFACE::Up()//Up Routine
{
+ printf("In up routine\n");
+ printf("Function value is: %d\n",_Function);
+ printf("Select Value is :%d\n",_Select);
if (_Function == 2 || _Function == 3 )//Coiling or twist Function
{
_Turns_Todo_mutex.lock();
- while(button_up.read() == 1)
- {
- _Turns_Todo = _Turns_Todo +1;
- Thread::wait(100);
- }
+ _Turns_Todo = _Turns_Todo +1;
_Turns_Todo_mutex.unlock();
}
else if (_Function == 0)//Annealing Function
{
if (_Select == 0)//Select = 0
{
- _Loop_mutex.lock();
- _Loop = _Loop + 1; //Increases Repetitions in Annealing
- _Loop_mutex.unlock();
+ if(_On_Time > 0)
+ {
+ _On_Time_mutex.lock();
+ _On_Time = _On_Time+ 1; //Increments On time
+ _On_Time_mutex.unlock();
+ }
}
else if (_Select == 1)//Select = 1
{
- _On_Time_mutex.lock();
- _On_Time = _On_Time + 1; //Increases On time
- _On_Time_mutex.unlock();
+ if(_Loop > 0)
+ {
+ _Loop_mutex.lock();
+ _Loop = _Loop + 1; //Increments Repetitions in Annealing
+ _Loop_mutex.unlock();
+ }
}
- else if (_Select == 2)//Select = 1
+ else if (_Select == 2)//Select = 2
{
- _Off_Time_mutex.lock();
- _Off_Time = _On_Time + 1; //Increases Off time
- _Off_Time_mutex.unlock();
+ if(_Off_Time > 0)
+ {
+ _Off_Time_mutex.lock();
+ _Off_Time = _Off_Time + 1; //Increments Off time
+ _Off_Time_mutex.unlock();
+ }
}
}
else if (_Function == 1)//Test Function
{
- if (_Select == 0)//Select = 0
+ if (_Select == 1)//Select = 1
{
if (_Duty_Cycle < 100)
{
@@ -138,7 +144,7 @@
_Duty_Cycle_mutex.unlock();
}
}
- else if (_Select == 1)//Select = 1
+ else if (_Select == 0)//Select = 0
{
_Power_Time_mutex.lock();
_Power_Time = _Power_Time + 1; //Increases time on
@@ -151,41 +157,42 @@
if (_Function == 2 || _Function == 3)//Coiling or twist Function
{
_Turns_Todo_mutex.lock();
- while(button_up.read() == 1)
- {
- _Turns_Todo = _Turns_Todo -1;
- Thread::wait(100);
- }
+ _Turns_Todo = _Turns_Todo -1;
_Turns_Todo_mutex.unlock();
}
- else if (_Function == 0)//Anneal Function
+ else if (_Function == 0)//Annealing Function
{
if (_Select == 0)//Select = 0
- {
-
- if (_Loop > 0)
+ {
+ if(_On_Time > 1)
{
- _Loop_mutex.lock();
- _Loop = _Loop - 1;
- _Loop_mutex.unlock();
+ _On_Time_mutex.lock();
+ _On_Time = _On_Time - 1; //Decrements On time
+ _On_Time_mutex.unlock();
}
}
else if (_Select == 1)//Select = 1
{
- _On_Time_mutex.lock();
- _On_Time = _On_Time - 1; //Decrease On time
- _On_Time_mutex.unlock();
+ if(_Loop > 1)
+ {
+ _Loop_mutex.lock();
+ _Loop = _Loop - 1; //Decrements Repetitions in Annealing
+ _Loop_mutex.unlock();
+ }
}
- else if (_Select == 2)//Select = 1
+ else if (_Select == 2)//Select = 2
{
- _Off_Time_mutex.lock();
- _Off_Time = _On_Time - 1; //Decrements Off time
- _Off_Time_mutex.unlock();
+ if(_Off_Time > 1)
+ {
+ _Off_Time_mutex.lock();
+ _Off_Time = _Off_Time - 1; //Decrements Off time
+ _Off_Time_mutex.unlock();
+ }
}
}
else if (_Function == 1)//Test Function
{
- if (_Select == 0)//Select = 0
+ if (_Select == 1)//Select = 1
{
if (_Duty_Cycle > 0)
{
@@ -194,7 +201,7 @@
_Duty_Cycle_mutex.unlock();
}
}
- else if (_Select == 1)//Select = 1
+ else if (_Select == 0)//Select = 0
{
if (_Power_Time > 0)
{
@@ -203,12 +210,10 @@
_Power_Time_mutex.unlock();
}
}
-
}
}
void INTERFACE::Start_Stop()//Start / Stop Routine
{
-
if(_System_Running != 2)
{
_System_Running = !_System_Running;
@@ -338,110 +343,123 @@
Function 2 = Coiling
Function 3 = Twisting
*/
-
+ if(Type_of_Rig == 3)
+ {
+ if(_Function < 3)
+ {
+ if(_Function == 1)//Skip coiling
+ {
+ _Function_mutex.lock();
+ _Function = _Function + 1;
+ _Function_mutex.unlock();
+ }
+ _Function_mutex.lock();
+ _Function = _Function + 1;
+ _Function_mutex.unlock();
+ }
+ else if(_Function == 3)
+ {
+ _Function = 0;
+ }
+ }
if(Type_of_Rig == 2)
{
if(_Function < 2)
{
_Function_mutex.lock();
- _Function = _Function + 1;
- _Function_mutex.unlock();
+ _Function = _Function + 1;
+ _Function_mutex.unlock();
}
else if(_Function == 2)
{
- _Function_mutex.lock();
- _Function = 0;
- _Function_mutex.unlock();
- }
- }
- else if(Type_of_Rig == 3)
- {
- if(_Function == 3)
- {
- _Function_mutex.lock();
- _Function = 0;
- _Function_mutex.unlock();
- }
- else if(_Function < 2)
- {
- _Function_mutex.lock();
- _Function = _Function + 1;
- _Function_mutex.unlock();
- }
- else if(_Function == 2)
- {
- _Function_mutex.lock();
- _Function = 3;
- _Function_mutex.unlock();
+ _Function = 0;
}
}
}
void INTERFACE::Select()//Select Routine
{
_Select_mutex.lock();
- if(_Select == 0 & _Function == !2)//All but anneal have 2 component menu's
- {
- _Select = 1;
- }
- else if(_Select == 1 & _Function == !2)//Toggle the value
+ if(_Function == 0)//Anneal
{
- _Select = 0;
- }
- else if(_Function == 1 & _Select < 2)
- {
- _Select = _Select + 1;
- }
- else if(_Select == 2 & _Function == 1)//Set select to 0 if the function is anneal, as anneal has 3 components in the menu
- {
- _Select = 0;
+ printf("Select Value is :%d\n",_Select);
+ if(_Select == 2)
+ {
+ _Select = 0;
+ }
+ else if(_Select == 1)
+ {
+ _Select = 2;
+ }
+ else if(_Select == 0)
+ {
+ _Select = 1;
+ }
+ if(_Select == 0 || _Select == 2)
+ {
+ Led_Select_Left_mutex.lock();
+ Led_Select_Left = 0;
+ Led_Select_Left_mutex.unlock();
+
+ Led_Select_Right_mutex.lock();
+ Led_Select_Right= 1;
+ Led_Select_Right_mutex.unlock();
+ }
+ else if(_Select == 1)
+ {
+ Led_Select_Left_mutex.lock();
+ Led_Select_Left = 1;
+ Led_Select_Left_mutex.unlock();
+
+ Led_Select_Right_mutex.lock();
+ Led_Select_Right= 0;
+ Led_Select_Right_mutex.unlock();
+ }
+ printf("Select Value is :%d\n",_Select);
}
- _Select_mutex.unlock();
- if(_Function == 0)
- {
- Led_Select_Left_mutex.lock();
- Led_Select_Right = 1;
- Led_Select_Left_mutex.unlock();
- }
- else if(_Function == 1 & _Select == false)
+ else if(_Function == 1)//Test
{
- Led_Select_Left_mutex.lock();
- Led_Select_Left = 1;
- Led_Select_Left_mutex.unlock();
+ if(_Select == 0)
+ {
+ _Select = 1;
+ }
+ else if(_Select == 1)
+ {
+ _Select = 0;
+ }
- Led_Select_Right_mutex.lock();
- Led_Select_Right= 0;
- Led_Select_Right_mutex.unlock();
+ if(_Select == 0)
+ {
+ Led_Select_Left_mutex.lock();
+ Led_Select_Left = 0;
+ Led_Select_Left_mutex.unlock();
+
+ Led_Select_Right_mutex.lock();
+ Led_Select_Right= 1;
+ Led_Select_Right_mutex.unlock();
+ }
+ else if(_Select ==1)
+ {
+ Led_Select_Left_mutex.lock();
+ Led_Select_Left = 1;
+ Led_Select_Left_mutex.unlock();
+
+ Led_Select_Right_mutex.lock();
+ Led_Select_Right= 0;
+ Led_Select_Right_mutex.unlock();
+ }
}
- else if(_Function == 1 & _Select == true)
+ else if(_Function == 2 || _Function == 3)
{
+ _Select = 0;
Led_Select_Left_mutex.lock();
Led_Select_Left = 0;
Led_Select_Left_mutex.unlock();
Led_Select_Right_mutex.lock();
Led_Select_Right= 1;
- Led_Select_Right_mutex.unlock();
+ Led_Select_Right_mutex.unlock();
}
- else if(_Function == 2 & _Select == false)
- {
- Led_Select_Left_mutex.lock();
- Led_Select_Left = 1;
- Led_Select_Left_mutex.unlock();
-
- Led_Select_Right_mutex.lock();
- Led_Select_Right= 0;
- Led_Select_Right_mutex.unlock();
- }
- else if(_Function == 2 & _Select == true)
- {
- Led_Select_Left_mutex.lock();
- Led_Select_Left = 0;
- Led_Select_Left_mutex.unlock();
-
- Led_Select_Right_mutex.lock();
- Led_Select_Right= 1;
- Led_Select_Right_mutex.unlock();
- }
+ _Select_mutex.unlock();
}
void INTERFACE::Interface_main()//Main Routine to execute commands when the start button has been pressed
{
--- a/Interface/Interface.hpp Mon Feb 18 22:04:54 2019 +0000 +++ b/Interface/Interface.hpp Tue Feb 19 14:40:01 2019 +0000 @@ -8,17 +8,17 @@ #define Default_Coiling_Turns 20 #define Default_Twisting_Turns 70 -//Interrupt In for the button control to the interface -static InterruptIn button_up(D8); //D8//Increment Button -static InterruptIn button_down(A1); //A1//Decrement Button -static InterruptIn button_start(D9); //D9//START / STOP BUTTON -static InterruptIn button_funct(A3); //A3//Function Button -static InterruptIn button_select(A4);//A4//Select Button +//Digital In for the button control to the interface +static DigitalIn button_up(D8); //D8//Increment Button +static DigitalIn button_down(A1); //A1//Decrement Button +static DigitalIn button_start(D9); //D9//START / STOP BUTTON +static DigitalIn button_function(A3); //A3//Function Button +static DigitalIn button_select(A4);//A4//Select Button //Led Outputs //check the pin outs -static DigitalOut Led_Select_Left(D2); -static DigitalOut Led_Select_Right(D3); +static DigitalOut Led_Select_Left(D3); +static DigitalOut Led_Select_Right(D2); static DigitalOut Led_Power(A2); static STEPPER_MOTOR STEPPER_MOTOR_1(D15,D14,D13,D12); static PwmOut Tendon_Power(PE_8);
--- a/SETUP.hpp Mon Feb 18 22:04:54 2019 +0000 +++ b/SETUP.hpp Tue Feb 19 14:40:01 2019 +0000 @@ -11,7 +11,7 @@ #include "STEPPER_MOTOR.hpp" #include "Interface.hpp" -#define Debounce_Time 50 +#define Debounce_Time 100//200 works fine INTERFACE INTERFACE; //If the code compiles GET IN THE BIN @@ -27,8 +27,9 @@ void start_thread_function(); void function_thread_function(); void select_thread_function(); - +void Polling_thread_function(); //Thread Setups +Thread polling_thread; Thread lcd_thread; Thread led_thread; Thread up_thread;
--- a/main.cpp Mon Feb 18 22:04:54 2019 +0000
+++ b/main.cpp Tue Feb 19 14:40:01 2019 +0000
@@ -1,18 +1,60 @@
#include "SETUP.hpp"
-
-//Thread signal functions
-void up_signal_rise(){up_thread.signal_set(1);} //Sets the up thread to operate
-void down_signal_rise(){down_thread.signal_set(1);} //Sets the down thread to operate
-void start_signal_rise(){start_stop_thread.signal_set(1);} //Sets the start thread to operate
-void function_signal_rise(){function_thread.signal_set(1);}//Sets the function thread to operate
-void select_signal_rise(){select_thread.signal_set(1);} //Sets the select thread to operate
-void up_signal_fall(){up_thread.signal_set(2);} //Sets the up thread to operate
-void down_signal_fall(){down_thread.signal_set(2);} //Sets the down thread to operate
-void start_signal_fall(){start_stop_thread.signal_set(2);} //Sets the start thread to operate
-void function_signal_fall(){function_thread.signal_set(2);}//Sets the function thread to operate
-void select_signal_fall(){select_thread.signal_set(2);} //Sets the select thread to operate
-
-void LCD_thread(){//Output data to the LCD for the interface
+void Polling_thread_function()// Polling thread for the buttons
+{
+ while(1)
+ {
+ //printf("In polling\n");//Check the value
+ if(button_up.read() == 0)
+ {
+ printf("Up button read\n");
+ Thread::wait(Debounce_Time);
+ if(button_up.read() == 1)
+ {
+ printf("Up button released\n");
+ up_thread.signal_set(1);
+ }
+ }
+ if(button_down.read() == 0)
+ {
+ printf("Down button read\n");
+ Thread::wait(Debounce_Time);
+ if(button_down.read() == 1)
+ {
+ down_thread.signal_set(1);
+ }
+ }
+ if(button_start.read() == 0)
+ {
+ printf("Start/Stop button read\n");
+ Thread::wait(Debounce_Time);
+ if(button_start.read() == 1)
+ {
+ start_stop_thread.signal_set(1);
+ }
+ }
+ if(button_function.read() == 0)
+ {
+ printf("Function button read\n");
+ Thread::wait(Debounce_Time);
+ if(button_function.read() == 1)
+ {
+ function_thread.signal_set(1);
+ }
+ }
+ if(button_select.read() == 0)
+ {
+ printf("Select button read\n");
+ Thread::wait(Debounce_Time);
+ if(button_select.read() == 1)
+ {
+ select_thread.signal_set(1);
+ }
+ }
+ Thread::wait(50);
+ }
+}
+void LCD_thread()//Output data to the LCD for the interface
+{
while(1)
{
lcd.cls();//Clear the LCD screen
@@ -35,12 +77,12 @@
}
else if(INTERFACE.Get_Function() == 2)//Coil
{
- lcd.printf("Mode: Turn\n");
+ lcd.printf("Mode: Coil\n");
lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do());
}
else if(INTERFACE.Get_Function() == 3)//Twist
{
- lcd.printf("Mode: Turn\n");
+ lcd.printf("Mode: Twist\n");
lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do());
}
Thread::wait(250);//Refresh rate for Lcd
@@ -65,81 +107,70 @@
*/
Thread::signal_wait(1); //Wait to be signaled by the up button interrupt
- Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
- if(INTERFACE.Get_Function() == 0 || INTERFACE.Get_Function() == 1)
- {
- Thread::signal_wait(2); //Wait to be signaled by the up button interrupt
- }
+ pc.printf("Up Button Pressed in thread function\n");//Output data to the putty terminal
+ Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs this debouce time might need to be longer
up_thread.signal_set(0); //Set the thread signal low to wait for the interrupt before reaccuring
pc.printf("Up Button Pressed\n");//Output data to the putty terminal
INTERFACE.Up(); //Run the interface up routine
- Thread::wait(1000); //Button debounce to prevent multiple re-runs
}
}
void down_thread_function() //Action if the down button is pressed decrement
{
while(1)
{
- Thread::signal_wait(1); //Wait to be signaled by the up button interrupt
+ /*
+ Thread::signal_wait(1); //Wait to be signaled by the down button interrupt
Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
- Thread::signal_wait(2); //Wait to be signaled by the up button interrupt
+ Thread::signal_wait(2); //Wait to be signaled by the down button interrupt
+
+ */
+ Thread::signal_wait(1); //Wait to be signaled by the down button interrupt
+ Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
down_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
pc.printf("Down Button Pressed\n");//Output data to the putty terminal
INTERFACE.Down();//Run the interface down routine
- Thread::wait(1000);//Button debounce to prevent multiple re-runs
}
}
void start_stop_thread_function() //Action if the Start/Stop button is pressed
{
while(1)
{
- Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
- Thread::signal_wait(2); //Wait to be signaled by the up button interrupt
+ Thread::signal_wait(1); //Wait to be signaled by the start / stop button interrupt
+ Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
pc.printf("S/S Button Pressed\n");//Output data to the putty terminal
INTERFACE.Start_Stop();//Run the interface start / stop routine
- Thread::wait(1000);//Button debounce to prevent multiple re-runs
+ //Thread::wait(1000);//Button debounce to prevent multiple re-runs
}
}
void Function_Selection_thread_function()//Action if the Function button is pressed
{
while(1)
{
- Thread::signal_wait(1); //Wait to be signaled by the up button interrupt
- Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
- Thread::signal_wait(2); //Wait to be signaled by the up button interrupt
+ Thread::signal_wait(1); //Wait to be signaled by the function button interrupt
+ Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
function_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
pc.printf("Function Button Pressed\n");//Output data to the putty terminal
INTERFACE.Function();//Run the interface function routine
- Thread::wait(2000);//Button debounce to prevent multiple re-runs
+ //Thread::wait(2000);//Button debounce to prevent multiple re-runs
}
}
void Selection_thread_function()//Action if the Select button is pressed
{
while(1)
{
- Thread::signal_wait(1); //Wait to be signaled by the up button interrupt
- Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
- Thread::signal_wait(2); //Wait to be signaled by the up button interrupt
+ Thread::signal_wait(1); //Wait to be signaled by the select button interrupt
+ Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
pc.printf("Select Button Pressed\n");//Output data to the putty terminal
INTERFACE.Select();//Run the interface selection routine
- Thread::wait(1000);//Button debounce to prevent multiple re-runs
+ //Thread::wait(1000);//Button debounce to prevent multiple re-runs
}
}
int main()
{
//Interrupt setters
- button_up.rise(&up_signal_rise); //Sets up Up button
- button_down.rise(&down_signal_rise); //Sets up Down Button
- button_start.rise(&start_signal_rise); //Sets up Start/Stop Button
- button_funct.rise(&function_signal_rise);//Sets up Function Button
- button_select.rise(&select_signal_rise); //Sets up Select Button
- button_up.fall(&up_signal_fall); //Sets up Up button
- button_down.fall(&down_signal_fall); //Sets up Down Button
- button_start.fall(&start_signal_fall); //Sets up Start/Stop Button
- button_funct.fall(&function_signal_fall);//Sets up Function Button
- button_select.fall(&select_signal_fall); //Sets up Select Button
+
lcd.printf("Ready Player\n");
lcd.printf(" One \n");
@@ -147,6 +178,7 @@
pc.printf("Program start\n");//Outputs informtation to the putty terminal
//Thread Starts
+ polling_thread.start(Polling_thread_function);//Start the button polling
lcd_thread.start(LCD_thread);//Output data to LCD
led_thread.start(LED_thread);//Blinking led to show CPU running
up_thread.start(up_thread_function);//UP interface thread