Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Committer:
thomasmorris
Date:
Mon Feb 11 16:58:07 2019 +0000
Revision:
14:63998be3d43c
Parent:
13:c681f340909b
Child:
15:7cf5595ed1b1
Working stepper needs fixing more;

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