This has increased comments and a readme

Dependencies:   mbed mbed-rtos TextLCD

Committer:
thomasmorris
Date:
Mon Feb 11 20:43:39 2019 +0000
Revision:
15:7cf5595ed1b1
Parent:
14:63998be3d43c
Child:
16:9f98ec0ededb
Coiling Rig V2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasmorris 14:63998be3d43c 1 #include "mbed.h"
thomasmorris 12:d9c133b360b0 2 #include "SETUP.hpp"
thomasmorris 12:d9c133b360b0 3 //Thread Functions
thomasmorris 14:63998be3d43c 4 DigitalOut led(LED1);
thomasmorris 14:63998be3d43c 5
thomasmorris 14:63998be3d43c 6 void up_signal(){up_thread.signal_set(1);} //Sets the up thread to operate
thomasmorris 14:63998be3d43c 7 void down_signal(){down_thread.signal_set(1);} //Sets the down thread to operate
thomasmorris 14:63998be3d43c 8 void start_signal(){start_stop_thread.signal_set(1);} //Sets the start thread to operate
thomasmorris 14:63998be3d43c 9 void function_signal(){function_thread.signal_set(1);}//Sets the function thread to operate
thomasmorris 14:63998be3d43c 10 void select_signal(){select_thread.signal_set(1);} //Sets the select thread to operate
thomasmorris 14:63998be3d43c 11
thomasmorris 12:d9c133b360b0 12 void LCD_thread(){
thomasmorris 12:d9c133b360b0 13 while(1){
thomasmorris 12:d9c133b360b0 14 lcd.cls();
thomasmorris 14:63998be3d43c 15 if(Function == 0){//Turn
thomasmorris 14:63998be3d43c 16 lcd.printf("Mode: Turn\n");
thomasmorris 14:63998be3d43c 17 lcd.printf("Done: %d ToDo: %d\n",turns_done,turns_todo);
thomasmorris 14:63998be3d43c 18 }else if(Function == 1){//Anneal
thomasmorris 12:d9c133b360b0 19 lcd.printf("Mode: Anneal\n");
thomasmorris 12:d9c133b360b0 20 lcd.printf("Loop:%d Wait:%d\n",Loop,Wait_Time);
thomasmorris 14:63998be3d43c 21
thomasmorris 14:63998be3d43c 22 }else if(Function == 2){//Test
thomasmorris 12:d9c133b360b0 23 lcd.printf("Mode: Test\n");
thomasmorris 12:d9c133b360b0 24 lcd.printf("Duty: %d Time: %d\n",Duty_Cycle,Power_Time);
thomasmorris 12:d9c133b360b0 25 }
thomasmorris 14:63998be3d43c 26 Thread::wait(250);
thomasmorris 12:d9c133b360b0 27 lcd.cls();
thomasmorris 12:d9c133b360b0 28 }
thomasmorris 12:d9c133b360b0 29 }
thomasmorris 12:d9c133b360b0 30 void LED_thread(){
thomasmorris 12:d9c133b360b0 31 while (1){
emilmont 1:491820ee784d 32 led2 = !led2;
mbed_official 11:0309bef74ba8 33 Thread::wait(1000);
emilmont 1:491820ee784d 34 }
emilmont 1:491820ee784d 35 }
thomasmorris 12:d9c133b360b0 36 //Interrupt functions
thomasmorris 14:63998be3d43c 37 void up_thread_function(){//Action if the up button is pressed increment
thomasmorris 14:63998be3d43c 38 while(1)
thomasmorris 14:63998be3d43c 39 {
thomasmorris 14:63998be3d43c 40 Thread::signal_wait(1);
thomasmorris 14:63998be3d43c 41 up_thread.signal_set(0);
thomasmorris 14:63998be3d43c 42 led1 = !led1;
thomasmorris 14:63998be3d43c 43 pc.printf("Up Button Pressed\n");
thomasmorris 14:63998be3d43c 44
thomasmorris 14:63998be3d43c 45 if (Function == 0){
thomasmorris 14:63998be3d43c 46
thomasmorris 14:63998be3d43c 47 No_Of_Rotations = No_Of_Rotations + 1;//Increases Turn number
thomasmorris 14:63998be3d43c 48 turns_todo = No_Of_Rotations;
thomasmorris 12:d9c133b360b0 49 }else if (Function == 1){
thomasmorris 12:d9c133b360b0 50 if (Select == false){
thomasmorris 14:63998be3d43c 51 Loop = Loop + 1; //Increases Repetitions in Annealing
thomasmorris 14:63998be3d43c 52 }else if (Select == true){
thomasmorris 14:63998be3d43c 53 Wait_Time = Wait_Time + 1; //Increases Wait time
thomasmorris 14:63998be3d43c 54 }
thomasmorris 14:63998be3d43c 55 }else if (Function == 2){
thomasmorris 14:63998be3d43c 56 if (Select == false){
thomasmorris 14:63998be3d43c 57 if (Duty_Cycle < 100){
thomasmorris 14:63998be3d43c 58 Duty_Cycle = Duty_Cycle + 10; //Increases Testing Duty Cycle
thomasmorris 14:63998be3d43c 59 }}else if (Select == true){
thomasmorris 14:63998be3d43c 60 Power_Time = Power_Time + 1; //Increases time on
thomasmorris 12:d9c133b360b0 61 }
thomasmorris 14:63998be3d43c 62 }
thomasmorris 14:63998be3d43c 63 Thread::wait(1000);//Button debounce
thomasmorris 14:63998be3d43c 64 }
thomasmorris 14:63998be3d43c 65 }
thomasmorris 14:63998be3d43c 66 void down_thread_function(){ //Action if the down button is pressed decrement
thomasmorris 14:63998be3d43c 67 while(1)
thomasmorris 14:63998be3d43c 68 {
thomasmorris 14:63998be3d43c 69 Thread::signal_wait(1);
thomasmorris 14:63998be3d43c 70 down_thread.signal_set(0);
thomasmorris 14:63998be3d43c 71 pc.printf("Down Button Pressed\n");
thomasmorris 14:63998be3d43c 72 if (Function == 0){
thomasmorris 14:63998be3d43c 73 No_Of_Rotations = No_Of_Rotations - 1;
thomasmorris 14:63998be3d43c 74 turns_todo = No_Of_Rotations;
thomasmorris 14:63998be3d43c 75 }else if (Function == 1){
thomasmorris 14:63998be3d43c 76 if (Select == false){
thomasmorris 14:63998be3d43c 77 if (Loop > 0){
thomasmorris 14:63998be3d43c 78 Loop = Loop - 1;
thomasmorris 14:63998be3d43c 79 }
thomasmorris 14:63998be3d43c 80 }else if (Select == true){
thomasmorris 14:63998be3d43c 81 if (Wait_Time > 0){
thomasmorris 14:63998be3d43c 82 Wait_Time = Wait_Time - 1;
thomasmorris 14:63998be3d43c 83 }
thomasmorris 14:63998be3d43c 84 }
thomasmorris 14:63998be3d43c 85 }else if (Function == 2){
thomasmorris 14:63998be3d43c 86 if (Select == false){
thomasmorris 14:63998be3d43c 87 if (Duty_Cycle > 0){
thomasmorris 14:63998be3d43c 88 Duty_Cycle = Duty_Cycle - 10; //Decreases duty
thomasmorris 14:63998be3d43c 89 }}else if (Select == true){
thomasmorris 14:63998be3d43c 90 if (Power_Time > 0){
thomasmorris 14:63998be3d43c 91 Power_Time = Power_Time - 1; //Decreases Time on
thomasmorris 12:d9c133b360b0 92 }
thomasmorris 14:63998be3d43c 93 }
thomasmorris 14:63998be3d43c 94 }
thomasmorris 14:63998be3d43c 95 Thread::wait(1000);//Button debounce
thomasmorris 12:d9c133b360b0 96 }
thomasmorris 12:d9c133b360b0 97 }
thomasmorris 14:63998be3d43c 98 void start_stop_thread_function(){ //Action if the Start/Stop button is pressed
thomasmorris 14:63998be3d43c 99 while(1)
thomasmorris 14:63998be3d43c 100 {
thomasmorris 14:63998be3d43c 101 Thread::signal_wait(1);
thomasmorris 14:63998be3d43c 102 start_stop_thread.signal_set(0);
thomasmorris 14:63998be3d43c 103 pc.printf("S/S Button Pressed\n");
thomasmorris 14:63998be3d43c 104 if (Function == 0)//Twist selected
thomasmorris 14:63998be3d43c 105 {
thomasmorris 14:63998be3d43c 106 Twist_Go = !Twist_Go;//toggle used for coiling and twisting
thomasmorris 14:63998be3d43c 107 }else if (Function == 1){ //Annealing selected
thomasmorris 14:63998be3d43c 108 Anneal_Go = !Anneal_Go;//toggle
thomasmorris 14:63998be3d43c 109 }else if (Function == 2){ //Testing selected
thomasmorris 14:63998be3d43c 110 Test_Go = !Test_Go;//toggle
thomasmorris 14:63998be3d43c 111 }
thomasmorris 14:63998be3d43c 112 Thread::wait(1000);//Button debounce
thomasmorris 14:63998be3d43c 113 }
thomasmorris 14:63998be3d43c 114 }
thomasmorris 14:63998be3d43c 115 void Function_Selection_thread_function() { //Action if the Function button is pressed
thomasmorris 14:63998be3d43c 116 while(1)
thomasmorris 14:63998be3d43c 117 {
thomasmorris 14:63998be3d43c 118 Thread::signal_wait(1);
thomasmorris 14:63998be3d43c 119 function_thread.signal_set(0);
thomasmorris 14:63998be3d43c 120 pc.printf("Function Button Pressed\n");
thomasmorris 14:63998be3d43c 121 if (Function < 2){
thomasmorris 14:63998be3d43c 122 Function = Function + 1;
thomasmorris 14:63998be3d43c 123 }else{
thomasmorris 14:63998be3d43c 124 Function = 0;
thomasmorris 14:63998be3d43c 125 }
thomasmorris 14:63998be3d43c 126 Thread::wait(2000);//Button debounce
emilmont 1:491820ee784d 127 }
emilmont 1:491820ee784d 128 }
thomasmorris 14:63998be3d43c 129 void Selection_thread_function() { //Action if the Select button is pressed
thomasmorris 14:63998be3d43c 130 while(1)
thomasmorris 14:63998be3d43c 131 {
thomasmorris 14:63998be3d43c 132 Thread::signal_wait(1);
thomasmorris 14:63998be3d43c 133 select_thread.signal_set(0);
thomasmorris 14:63998be3d43c 134 pc.printf("Select Button Pressed\n");
thomasmorris 14:63998be3d43c 135 Select = !Select;
thomasmorris 14:63998be3d43c 136 Thread::wait(1000);//Button debounce
thomasmorris 14:63998be3d43c 137 }
thomasmorris 12:d9c133b360b0 138 }
thomasmorris 14:63998be3d43c 139
thomasmorris 12:d9c133b360b0 140 int main()
thomasmorris 12:d9c133b360b0 141 {
thomasmorris 14:63998be3d43c 142
thomasmorris 12:d9c133b360b0 143 //Interrupt setters
thomasmorris 14:63998be3d43c 144 button_up.rise(&up_signal); //Sets up Up button
thomasmorris 14:63998be3d43c 145 button_down.rise(&down_signal); //Sets up Down Button
thomasmorris 14:63998be3d43c 146 button_start.rise(&start_signal); //Sets up Start/Stop Button
thomasmorris 14:63998be3d43c 147 button_funct.rise(&function_signal); //Sets up Function Button
thomasmorris 14:63998be3d43c 148 button_select.rise(&select_signal); //Sets up Select Button
thomasmorris 14:63998be3d43c 149
thomasmorris 14:63998be3d43c 150 //Thread Starts
thomasmorris 14:63998be3d43c 151 lcd_thread.start(LCD_thread);
thomasmorris 14:63998be3d43c 152 led_thread.start(LED_thread);
thomasmorris 14:63998be3d43c 153
thomasmorris 14:63998be3d43c 154 up_thread.start(up_thread_function);
thomasmorris 14:63998be3d43c 155 start_stop_thread.start(start_stop_thread_function);
thomasmorris 14:63998be3d43c 156
thomasmorris 14:63998be3d43c 157 down_thread.start(down_thread_function);
thomasmorris 14:63998be3d43c 158 function_thread.start(Function_Selection_thread_function);
thomasmorris 14:63998be3d43c 159 select_thread.start(Selection_thread_function);
thomasmorris 14:63998be3d43c 160
thomasmorris 12:d9c133b360b0 161 No_Of_Rotations = 20;//Defaults at 20 as a starting value
thomasmorris 14:63998be3d43c 162 turns_todo = No_Of_Rotations;
thomasmorris 12:d9c133b360b0 163 Twist_Go = false; //
thomasmorris 12:d9c133b360b0 164 Anneal_Go = false; // The Values for the start/ stop button on each select setting
thomasmorris 12:d9c133b360b0 165 Test_Go = false; //
thomasmorris 12:d9c133b360b0 166
thomasmorris 12:d9c133b360b0 167 Function = 0; //These values are used to navigate the differing modes
thomasmorris 12:d9c133b360b0 168 Select = false; //
thomasmorris 12:d9c133b360b0 169
thomasmorris 12:d9c133b360b0 170 Loop = 8; //Default loops
thomasmorris 12:d9c133b360b0 171 Wait_Time = 6; //Default wait time
thomasmorris 12:d9c133b360b0 172
thomasmorris 12:d9c133b360b0 173 Duty_Cycle = 50; //Percent
thomasmorris 12:d9c133b360b0 174 Power_Time = 6; //Seconds
thomasmorris 12:d9c133b360b0 175
thomasmorris 12:d9c133b360b0 176 /*
thomasmorris 14:63998be3d43c 177 Function 0 = Turn
thomasmorris 14:63998be3d43c 178 Function 1 = Anneal
thomasmorris 12:d9c133b360b0 179 Function 2 = Test
thomasmorris 12:d9c133b360b0 180 */
thomasmorris 14:63998be3d43c 181
thomasmorris 14:63998be3d43c 182 pc.printf("Program start\n");//Outputs informtation to the putty terminal
thomasmorris 14:63998be3d43c 183
thomasmorris 14:63998be3d43c 184
thomasmorris 12:d9c133b360b0 185 while(1) // Main code
thomasmorris 12:d9c133b360b0 186 {
thomasmorris 12:d9c133b360b0 187 if (Function == 0){ //Turning Code
thomasmorris 12:d9c133b360b0 188 if (Twist_Go == true){
thomasmorris 13:c681f340909b 189 STEPPER_MOTOR_1.Rotate_Steps(No_Of_Rotations);//Rotates for the specified number of steps given
thomasmorris 12:d9c133b360b0 190 }
thomasmorris 12:d9c133b360b0 191 }else if (Function == 1){ //Annealing Code
thomasmorris 12:d9c133b360b0 192 if (Anneal_Go == true)
thomasmorris 12:d9c133b360b0 193 {
thomasmorris 12:d9c133b360b0 194 for ( int counter = 0; counter < Loop; counter++) //Loop value, check if works
thomasmorris 12:d9c133b360b0 195 {
thomasmorris 12:d9c133b360b0 196 Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
thomasmorris 12:d9c133b360b0 197 Tendon_Power=1; // set duty cycle to 100%
thomasmorris 12:d9c133b360b0 198 wait(Wait_Time);//Variable
thomasmorris 12:d9c133b360b0 199 Tendon_Power=0; // set duty cycle to 0%
thomasmorris 12:d9c133b360b0 200 wait(6);//Fixed off time
thomasmorris 12:d9c133b360b0 201 }
thomasmorris 12:d9c133b360b0 202 }
thomasmorris 12:d9c133b360b0 203 }else if (Function == 2){ //Testing Code
thomasmorris 12:d9c133b360b0 204 if (Test_Go == true)
thomasmorris 12:d9c133b360b0 205 {
thomasmorris 12:d9c133b360b0 206 Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
thomasmorris 12:d9c133b360b0 207 Tendon_Power= Duty_Cycle / 100; // set duty cycle to variable input from buttons between 0-1 (on lcd this is a percentage) also increment by values of 10
thomasmorris 12:d9c133b360b0 208 wait(Power_Time);//Variable on time for power on
thomasmorris 12:d9c133b360b0 209 Tendon_Power=0; // set duty cycle to 0% and power off
thomasmorris 12:d9c133b360b0 210 }
thomasmorris 12:d9c133b360b0 211 }
thomasmorris 12:d9c133b360b0 212 }
thomasmorris 12:d9c133b360b0 213 }
thomasmorris 14:63998be3d43c 214
thomasmorris 14:63998be3d43c 215