Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
main.cpp@54:a4c5949707ca, 2018-05-16 (annotated)
- Committer:
- thomasmorris
- Date:
- Wed May 16 19:57:52 2018 +0000
- Revision:
- 54:a4c5949707ca
- Parent:
- 53:71f59e195f06
- Child:
- 55:e0e684531825
Working_data_receive
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 53:71f59e195f06 | 1 | |
thomasmorris | 25:36699ed589ab | 2 | #include "SETUP.hpp" |
thomasmorris | 52:99915f5240b2 | 3 | |
thomasmorris | 54:a4c5949707ca | 4 | #define Cubelet_data_ready 1 |
thomasmorris | 54:a4c5949707ca | 5 | #define Not_Cubelet_data_ready 0 |
thomasmorris | 54:a4c5949707ca | 6 | /* |
thomasmorris | 54:a4c5949707ca | 7 | Colour lookup table |
thomasmorris | 54:a4c5949707ca | 8 | 1 = White |
thomasmorris | 54:a4c5949707ca | 9 | 2 = Red |
thomasmorris | 54:a4c5949707ca | 10 | 3 = Orange |
thomasmorris | 54:a4c5949707ca | 11 | 4 = Blue |
thomasmorris | 54:a4c5949707ca | 12 | 5 = Green |
thomasmorris | 54:a4c5949707ca | 13 | 6 = Yellow |
thomasmorris | 54:a4c5949707ca | 14 | */ |
thomasmorris | 54:a4c5949707ca | 15 | |
thomasmorris | 54:a4c5949707ca | 16 | //Colours Cubelet_Colours_string[]; |
thomasmorris | 53:71f59e195f06 | 17 | int SPI_RX_DATA = 0; |
thomasmorris | 53:71f59e195f06 | 18 | int Cubelet_Colours[9] = {0,0,0,0,0,0,0,0,0};//9 cubelets store colours here |
thomasmorris | 53:71f59e195f06 | 19 | int Received_data = 0; |
thomasmorris | 53:71f59e195f06 | 20 | bool Data_from_slave[16] = {0}; |
thomasmorris | 53:71f59e195f06 | 21 | |
thomasmorris | 52:99915f5240b2 | 22 | //Interrupt service routine for handling the timeout of SW1 |
thomasmorris | 52:99915f5240b2 | 23 | void SW1TimeOutHandler() { |
thomasmorris | 52:99915f5240b2 | 24 | SW1TimeOut.detach(); //Stop the timeout counter firing |
thomasmorris | 52:99915f5240b2 | 25 | SW1.fall(&SW1FallingEdge); //Now wait for a falling edge |
thomasmorris | 52:99915f5240b2 | 26 | } |
thomasmorris | 52:99915f5240b2 | 27 | //Interrupt service routive for SW2 falling edge (release) |
thomasmorris | 52:99915f5240b2 | 28 | void SW1FallingEdge() { |
thomasmorris | 52:99915f5240b2 | 29 | SW1.fall(NULL); //Disable this interrupt |
thomasmorris | 52:99915f5240b2 | 30 | SW1TimeOut.attach(&SW1TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter |
thomasmorris | 52:99915f5240b2 | 31 | } |
thomasmorris | 52:99915f5240b2 | 32 | //Interrupt service routine for handling the timeout of SW2 |
thomasmorris | 52:99915f5240b2 | 33 | void SW2TimeOutHandler() { |
thomasmorris | 52:99915f5240b2 | 34 | SW2TimeOut.detach(); //Stop the timeout counter firing |
thomasmorris | 52:99915f5240b2 | 35 | SW2.fall(&SW2FallingEdge); //Now wait for a falling edge |
thomasmorris | 52:99915f5240b2 | 36 | } |
thomasmorris | 52:99915f5240b2 | 37 | //Interrupt service routive for SW2 falling edge (release) |
thomasmorris | 52:99915f5240b2 | 38 | void SW2FallingEdge() { |
thomasmorris | 52:99915f5240b2 | 39 | SW2.fall(NULL); //Disable this interrupt |
thomasmorris | 52:99915f5240b2 | 40 | SW2TimeOut.attach(&SW2TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter |
thomasmorris | 52:99915f5240b2 | 41 | } |
thomasmorris | 47:6d128e500875 | 42 | void LCD_Output() |
thomasmorris | 30:4cde05cc7c4f | 43 | { |
thomasmorris | 30:4cde05cc7c4f | 44 | while(1) |
thomasmorris | 29:64b1f95a807c | 45 | { |
thomasmorris | 53:71f59e195f06 | 46 | //Write to the LCD |
thomasmorris | 7:dfe19413fdc2 | 47 | } |
thomasmorris | 7:dfe19413fdc2 | 48 | } |
thomasmorris | 47:6d128e500875 | 49 | void Serial_Commands() |
thomasmorris | 22:eb4cc12087b2 | 50 | { |
thomasmorris | 47:6d128e500875 | 51 | while(1) |
thomasmorris | 47:6d128e500875 | 52 | { |
thomasmorris | 52:99915f5240b2 | 53 | Serial_Commands_Output(); //Enable Serial Commands |
thomasmorris | 47:6d128e500875 | 54 | } |
thomasmorris | 22:eb4cc12087b2 | 55 | } |
thomasmorris | 48:244d6d81bb52 | 56 | void LED_Logging() |
thomasmorris | 48:244d6d81bb52 | 57 | { |
thomasmorris | 48:244d6d81bb52 | 58 | while(1) |
thomasmorris | 48:244d6d81bb52 | 59 | { |
thomasmorris | 52:99915f5240b2 | 60 | Log_Leds(); //Flashes the yellow led to indicate the logging mode |
thomasmorris | 48:244d6d81bb52 | 61 | } |
thomasmorris | 48:244d6d81bb52 | 62 | } |
thomasmorris | 53:71f59e195f06 | 63 | |
thomasmorris | 53:71f59e195f06 | 64 | void SPI_INTERFACE() |
thomasmorris | 53:71f59e195f06 | 65 | { |
thomasmorris | 53:71f59e195f06 | 66 | //pc.printf("SPI Test \n"); |
thomasmorris | 53:71f59e195f06 | 67 | Thread::wait(1000); |
thomasmorris | 53:71f59e195f06 | 68 | while(1) |
thomasmorris | 53:71f59e195f06 | 69 | { |
thomasmorris | 53:71f59e195f06 | 70 | //Do stuff |
thomasmorris | 53:71f59e195f06 | 71 | cs= 0; |
thomasmorris | 54:a4c5949707ca | 72 | SPI_RX_DATA = spi.write(0xF0); |
thomasmorris | 53:71f59e195f06 | 73 | wait_us(3); |
thomasmorris | 53:71f59e195f06 | 74 | cs= 1; |
thomasmorris | 53:71f59e195f06 | 75 | //SPI_RX_DATA = SPI_RX_DATA >> 7; |
thomasmorris | 53:71f59e195f06 | 76 | //SPI_RX_DATA = SPI_RX_DATA << 1; |
thomasmorris | 54:a4c5949707ca | 77 | //std::bitset<16> bits(SPI_RX_DATA); |
thomasmorris | 54:a4c5949707ca | 78 | //cout << bits << endl; |
thomasmorris | 54:a4c5949707ca | 79 | |
thomasmorris | 54:a4c5949707ca | 80 | //if(bits != 0) |
thomasmorris | 54:a4c5949707ca | 81 | //{ |
thomasmorris | 54:a4c5949707ca | 82 | //find the location possition |
thomasmorris | 54:a4c5949707ca | 83 | //find the colour value |
thomasmorris | 54:a4c5949707ca | 84 | //Store these values in to the tempory array |
thomasmorris | 54:a4c5949707ca | 85 | //When all of the data is received all 9 cubelets set a global variable high / thread signal |
thomasmorris | 54:a4c5949707ca | 86 | |
thomasmorris | 54:a4c5949707ca | 87 | //} |
thomasmorris | 53:71f59e195f06 | 88 | pc.printf("Received data = %d\n", SPI_RX_DATA); |
thomasmorris | 53:71f59e195f06 | 89 | //wait_us(3); |
thomasmorris | 53:71f59e195f06 | 90 | Thread::wait(1000); |
thomasmorris | 53:71f59e195f06 | 91 | |
thomasmorris | 53:71f59e195f06 | 92 | } |
thomasmorris | 53:71f59e195f06 | 93 | } |
thomasmorris | 54:a4c5949707ca | 94 | |
thomasmorris | 54:a4c5949707ca | 95 | Colours convert (int Colour) |
thomasmorris | 54:a4c5949707ca | 96 | { |
thomasmorris | 54:a4c5949707ca | 97 | Colours colours_return_value; |
thomasmorris | 54:a4c5949707ca | 98 | if(Colour == 1) |
thomasmorris | 54:a4c5949707ca | 99 | { |
thomasmorris | 54:a4c5949707ca | 100 | colours_return_value = White; |
thomasmorris | 54:a4c5949707ca | 101 | } |
thomasmorris | 54:a4c5949707ca | 102 | else if(Colour == 2) |
thomasmorris | 54:a4c5949707ca | 103 | { |
thomasmorris | 54:a4c5949707ca | 104 | colours_return_value = Red; |
thomasmorris | 54:a4c5949707ca | 105 | } |
thomasmorris | 54:a4c5949707ca | 106 | else if(Colour == 3) |
thomasmorris | 54:a4c5949707ca | 107 | { |
thomasmorris | 54:a4c5949707ca | 108 | colours_return_value = Orange; |
thomasmorris | 54:a4c5949707ca | 109 | } |
thomasmorris | 54:a4c5949707ca | 110 | else if(Colour == 4) |
thomasmorris | 54:a4c5949707ca | 111 | { |
thomasmorris | 54:a4c5949707ca | 112 | colours_return_value = Blue; |
thomasmorris | 54:a4c5949707ca | 113 | } |
thomasmorris | 54:a4c5949707ca | 114 | else if(Colour == 5) |
thomasmorris | 54:a4c5949707ca | 115 | { |
thomasmorris | 54:a4c5949707ca | 116 | colours_return_value = Green; |
thomasmorris | 54:a4c5949707ca | 117 | } |
thomasmorris | 54:a4c5949707ca | 118 | else if(Colour == 6) |
thomasmorris | 54:a4c5949707ca | 119 | { |
thomasmorris | 54:a4c5949707ca | 120 | colours_return_value = Yellow; |
thomasmorris | 54:a4c5949707ca | 121 | } |
thomasmorris | 54:a4c5949707ca | 122 | |
thomasmorris | 54:a4c5949707ca | 123 | return colours_return_value; |
thomasmorris | 54:a4c5949707ca | 124 | } |
thomasmorris | 54:a4c5949707ca | 125 | void Store_Cubelet_data() |
thomasmorris | 54:a4c5949707ca | 126 | { |
thomasmorris | 54:a4c5949707ca | 127 | |
thomasmorris | 54:a4c5949707ca | 128 | /* |
thomasmorris | 54:a4c5949707ca | 129 | enum Colours//The posible colours of each point on the cube map |
thomasmorris | 54:a4c5949707ca | 130 | { |
thomasmorris | 54:a4c5949707ca | 131 | White,//Front |
thomasmorris | 54:a4c5949707ca | 132 | Red,//Upper |
thomasmorris | 54:a4c5949707ca | 133 | Orange,//Down |
thomasmorris | 54:a4c5949707ca | 134 | Blue,//Left |
thomasmorris | 54:a4c5949707ca | 135 | Green,//Right |
thomasmorris | 54:a4c5949707ca | 136 | Yellow//Back |
thomasmorris | 54:a4c5949707ca | 137 | }; |
thomasmorris | 54:a4c5949707ca | 138 | |
thomasmorris | 54:a4c5949707ca | 139 | */ |
thomasmorris | 54:a4c5949707ca | 140 | |
thomasmorris | 54:a4c5949707ca | 141 | //signal::wait(Cubelet_data_ready); |
thomasmorris | 54:a4c5949707ca | 142 | if(Cubelet_Colours[5] == 1) |
thomasmorris | 54:a4c5949707ca | 143 | { |
thomasmorris | 54:a4c5949707ca | 144 | CubeMap[0][0][0] = convert(Cubelet_Colours[0]); |
thomasmorris | 54:a4c5949707ca | 145 | CubeMap[0][0][1] = convert(Cubelet_Colours[1]); |
thomasmorris | 54:a4c5949707ca | 146 | CubeMap[0][0][2] = convert(Cubelet_Colours[2]); |
thomasmorris | 54:a4c5949707ca | 147 | CubeMap[0][1][0] = convert(Cubelet_Colours[3]); |
thomasmorris | 54:a4c5949707ca | 148 | CubeMap[0][1][1] = convert(Cubelet_Colours[4]); |
thomasmorris | 54:a4c5949707ca | 149 | CubeMap[0][1][2] = convert(Cubelet_Colours[5]); |
thomasmorris | 54:a4c5949707ca | 150 | CubeMap[0][2][0] = convert(Cubelet_Colours[6]); |
thomasmorris | 54:a4c5949707ca | 151 | CubeMap[0][2][1] = convert(Cubelet_Colours[7]); |
thomasmorris | 54:a4c5949707ca | 152 | CubeMap[0][2][2] = convert(Cubelet_Colours[8]); |
thomasmorris | 54:a4c5949707ca | 153 | //CubeMap[6][3][3] |
thomasmorris | 54:a4c5949707ca | 154 | //Store this to the white face |
thomasmorris | 54:a4c5949707ca | 155 | } |
thomasmorris | 54:a4c5949707ca | 156 | } |
thomasmorris | 54:a4c5949707ca | 157 | |
thomasmorris | 53:71f59e195f06 | 158 | void Motor_Control() |
thomasmorris | 5:2594b953f111 | 159 | { |
thomasmorris | 53:71f59e195f06 | 160 | while(1) |
thomasmorris | 53:71f59e195f06 | 161 | { |
thomasmorris | 53:71f59e195f06 | 162 | if(Motor_To_Select !=0 and steps !=0) |
thomasmorris | 53:71f59e195f06 | 163 | { |
thomasmorris | 53:71f59e195f06 | 164 | if(Motor_To_Select == 1) |
thomasmorris | 53:71f59e195f06 | 165 | { |
thomasmorris | 53:71f59e195f06 | 166 | STEPPER_MOTOR_1.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 167 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 168 | } |
thomasmorris | 53:71f59e195f06 | 169 | else if(Motor_To_Select == 2) |
thomasmorris | 53:71f59e195f06 | 170 | { |
thomasmorris | 53:71f59e195f06 | 171 | STEPPER_MOTOR_2.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 172 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 173 | } |
thomasmorris | 53:71f59e195f06 | 174 | else if(Motor_To_Select == 3) |
thomasmorris | 53:71f59e195f06 | 175 | { |
thomasmorris | 53:71f59e195f06 | 176 | STEPPER_MOTOR_3.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 177 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 178 | } |
thomasmorris | 53:71f59e195f06 | 179 | else if(Motor_To_Select == 4) |
thomasmorris | 53:71f59e195f06 | 180 | { |
thomasmorris | 53:71f59e195f06 | 181 | STEPPER_MOTOR_4.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 182 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 183 | } |
thomasmorris | 53:71f59e195f06 | 184 | else if(Motor_To_Select == 5) |
thomasmorris | 53:71f59e195f06 | 185 | { |
thomasmorris | 53:71f59e195f06 | 186 | STEPPER_MOTOR_5.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 187 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 188 | } |
thomasmorris | 53:71f59e195f06 | 189 | |
thomasmorris | 53:71f59e195f06 | 190 | else if(Motor_To_Select == 6) |
thomasmorris | 53:71f59e195f06 | 191 | { |
thomasmorris | 53:71f59e195f06 | 192 | STEPPER_MOTOR_6.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 193 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 194 | } |
thomasmorris | 53:71f59e195f06 | 195 | |
thomasmorris | 53:71f59e195f06 | 196 | } |
thomasmorris | 53:71f59e195f06 | 197 | } |
thomasmorris | 25:36699ed589ab | 198 | } |
thomasmorris | 53:71f59e195f06 | 199 | |
thomasmorris | 25:36699ed589ab | 200 | int main() |
thomasmorris | 25:36699ed589ab | 201 | { |
thomasmorris | 52:99915f5240b2 | 202 | set_time(1515530152); //Sets time |
thomasmorris | 52:99915f5240b2 | 203 | pc.baud(9600); //Sets the Serial Comms Baud Rate |
thomasmorris | 53:71f59e195f06 | 204 | //SPI_INIT(); |
thomasmorris | 53:71f59e195f06 | 205 | cs = 1; //Active Low |
thomasmorris | 53:71f59e195f06 | 206 | |
thomasmorris | 53:71f59e195f06 | 207 | // Setup the spi for 8 bit data, high steady state clock, |
thomasmorris | 53:71f59e195f06 | 208 | // second edge capture, with a 1MHz clock rate |
thomasmorris | 53:71f59e195f06 | 209 | spi.format(16,1); // 8 Data bits phase 0 polarity 0 |
thomasmorris | 53:71f59e195f06 | 210 | spi.frequency(1000000);//Output clock frequency 1Mhz |
thomasmorris | 53:71f59e195f06 | 211 | //post(); //Power on Self Test |
thomasmorris | 25:36699ed589ab | 212 | |
thomasmorris | 52:99915f5240b2 | 213 | //Start Threads |
thomasmorris | 53:71f59e195f06 | 214 | t1.start(Motor_Control); |
thomasmorris | 53:71f59e195f06 | 215 | t2.start(SPI_INTERFACE); |
thomasmorris | 53:71f59e195f06 | 216 | t3.start(Serial_Commands); |
thomasmorris | 52:99915f5240b2 | 217 | //Interrupts |
thomasmorris | 52:99915f5240b2 | 218 | SW1.fall(&SW1FallingEdge); |
thomasmorris | 52:99915f5240b2 | 219 | SW2.fall(&SW2FallingEdge); |
thomasmorris | 52:99915f5240b2 | 220 | |
thomasmorris | 5:2594b953f111 | 221 | //Main thread ID |
thomasmorris | 5:2594b953f111 | 222 | idMain = osThreadGetId(); //CMSIS RTOS call |
thomasmorris | 25:36699ed589ab | 223 | |
thomasmorris | 53:71f59e195f06 | 224 | |
thomasmorris | 5:2594b953f111 | 225 | //Thread ID |
thomasmorris | 5:2594b953f111 | 226 | id1 = t1.gettid(); |
thomasmorris | 5:2594b953f111 | 227 | id2 = t2.gettid(); |
thomasmorris | 21:3c078c799caa | 228 | id3 = t3.gettid(); |
thomasmorris | 21:3c078c799caa | 229 | id4 = t4.gettid(); |
chills | 24:7d2da96e05ad | 230 | id5 = t5.gettid(); |
thomasmorris | 48:244d6d81bb52 | 231 | id6 = t6.gettid(); |
thomasmorris | 53:71f59e195f06 | 232 | |
thomasmorris | 48:244d6d81bb52 | 233 | while(true) |
thomasmorris | 48:244d6d81bb52 | 234 | { |
thomasmorris | 53:71f59e195f06 | 235 | //Do nothing main thread will sleep |
noutram | 0:36e89e3ed7c4 | 236 | } |
thomasmorris | 45:875f7e18a386 | 237 | } |