First Publish

Dependencies:   BridgeDriver2 FrontPanelButtons MAX31855 MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Mon Nov 10 22:59:49 2014 +0000
Revision:
1:9954bf6d7d25
Parent:
0:20e78c9d2ea9
First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mehatfie 0:20e78c9d2ea9 1 #include "mbed.h"
mehatfie 0:20e78c9d2ea9 2 #include "BridgeDriver.h"
mehatfie 0:20e78c9d2ea9 3 #include "FrontPanelButtons.h"
mehatfie 0:20e78c9d2ea9 4 #include "SDFileSystem.h"
mehatfie 0:20e78c9d2ea9 5 #include "LocalPinNames.h"
mehatfie 0:20e78c9d2ea9 6 #include "TextLCD.h"
mehatfie 0:20e78c9d2ea9 7 #include "max31855.h"
mehatfie 0:20e78c9d2ea9 8 #include <string>
mehatfie 0:20e78c9d2ea9 9 #include <stdio.h>
mehatfie 0:20e78c9d2ea9 10 using std::string;
mehatfie 0:20e78c9d2ea9 11
mehatfie 0:20e78c9d2ea9 12 #include "DS18S20.h"
mehatfie 0:20e78c9d2ea9 13 #include "DS18B20.h"
mehatfie 0:20e78c9d2ea9 14 #include "OneWireDefs.h"
mehatfie 0:20e78c9d2ea9 15
mehatfie 0:20e78c9d2ea9 16 #define THERMOMETER DS18B20
mehatfie 0:20e78c9d2ea9 17 // device( crcOn, useAddress, parasitic, mbed pin )
mehatfie 0:20e78c9d2ea9 18 THERMOMETER device(true, true, false, p25);
mehatfie 0:20e78c9d2ea9 19
mehatfie 0:20e78c9d2ea9 20 Serial pc(USBTX, USBRX);
mehatfie 0:20e78c9d2ea9 21
mehatfie 0:20e78c9d2ea9 22 Timer timer; // general purpose timer
mehatfie 0:20e78c9d2ea9 23 I2C i2c( P0_10, P0_11 ); // I2C bus (SDA, SCL)
mehatfie 0:20e78c9d2ea9 24 //TextLCD_I2C lcd( &i2c, MCP23008_SA0, TextLCD::lcd20x4 ); // lcd
mehatfie 0:20e78c9d2ea9 25 FrontPanelButtons buttons( &i2c ); // front panel buttons
mehatfie 0:20e78c9d2ea9 26
mehatfie 0:20e78c9d2ea9 27 Timer debounceTimer;
mehatfie 0:20e78c9d2ea9 28 Timer avgCycTimer;
mehatfie 0:20e78c9d2ea9 29
mehatfie 0:20e78c9d2ea9 30 DigitalIn signal(DIO0, PullUp);
mehatfie 0:20e78c9d2ea9 31 CAN can(p9, p10);
mehatfie 0:20e78c9d2ea9 32
mehatfie 0:20e78c9d2ea9 33 //SPI Interfaces
mehatfie 0:20e78c9d2ea9 34 SPI testSPI(P0_9, P0_8, P0_7); // mosi(out), miso(in), sclk(clock) //SPI Bus
mehatfie 0:20e78c9d2ea9 35 //SPI testSPI(P0_9, DIO3, DIO1); // mosi(out), miso(in), sclk(clock) //SPI Bus
mehatfie 0:20e78c9d2ea9 36
mehatfie 0:20e78c9d2ea9 37
mehatfie 0:20e78c9d2ea9 38 CANMessage readBuffer;
mehatfie 0:20e78c9d2ea9 39 int numBuffMsg = 0;
mehatfie 0:20e78c9d2ea9 40
mehatfie 0:20e78c9d2ea9 41 float totaltime = 0;
mehatfie 0:20e78c9d2ea9 42 int cycleCount = 1;
mehatfie 0:20e78c9d2ea9 43 int numCycles = 1000;
mehatfie 0:20e78c9d2ea9 44
mehatfie 0:20e78c9d2ea9 45 float currTemp = 0; //Float value to hold temperature returned
mehatfie 0:20e78c9d2ea9 46
mehatfie 0:20e78c9d2ea9 47 //Function Definitions
mehatfie 0:20e78c9d2ea9 48 void waitSwitch(char [], int);
mehatfie 0:20e78c9d2ea9 49 void waitSwitchRelease(char [], int);
mehatfie 0:20e78c9d2ea9 50 void waitLatch(char [], int);
mehatfie 0:20e78c9d2ea9 51 int checkLatchStatus();
mehatfie 0:20e78c9d2ea9 52 float getTemp();
mehatfie 0:20e78c9d2ea9 53
mehatfie 0:20e78c9d2ea9 54
mehatfie 0:20e78c9d2ea9 55 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 56 /************ CAN Commands *************/
mehatfie 0:20e78c9d2ea9 57 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 58
mehatfie 0:20e78c9d2ea9 59 void openDoorCommand(char dataBytes[]){
mehatfie 0:20e78c9d2ea9 60 dataBytes[3] = 0x03;
mehatfie 0:20e78c9d2ea9 61 can.write(CANMessage(534, dataBytes, 4)); // open the door
mehatfie 0:20e78c9d2ea9 62 wait(0.1);
mehatfie 0:20e78c9d2ea9 63 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 64 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 65 }
mehatfie 0:20e78c9d2ea9 66
mehatfie 0:20e78c9d2ea9 67 void closeDoorCommand(char dataBytes[]){
mehatfie 0:20e78c9d2ea9 68 dataBytes[3] = 0x02;
mehatfie 0:20e78c9d2ea9 69 can.write(CANMessage(534, dataBytes, 4)); // close the door
mehatfie 0:20e78c9d2ea9 70 wait(0.1);
mehatfie 0:20e78c9d2ea9 71 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 72 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 73 }
mehatfie 0:20e78c9d2ea9 74
mehatfie 0:20e78c9d2ea9 75 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 76 /************ Receieve *************/
mehatfie 0:20e78c9d2ea9 77 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 78
mehatfie 0:20e78c9d2ea9 79 void receive(){
mehatfie 0:20e78c9d2ea9 80 CANMessage msg;
mehatfie 0:20e78c9d2ea9 81 if (can.read(msg)){
mehatfie 0:20e78c9d2ea9 82 if (msg.id == 534){
mehatfie 0:20e78c9d2ea9 83 readBuffer = msg;
mehatfie 0:20e78c9d2ea9 84 numBuffMsg = 1;
mehatfie 0:20e78c9d2ea9 85 }
mehatfie 0:20e78c9d2ea9 86 }
mehatfie 0:20e78c9d2ea9 87 }
mehatfie 0:20e78c9d2ea9 88
mehatfie 0:20e78c9d2ea9 89
mehatfie 0:20e78c9d2ea9 90 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 91 /************ Full Init *************/
mehatfie 0:20e78c9d2ea9 92 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 93
mehatfie 0:20e78c9d2ea9 94 void fullInit(){
mehatfie 0:20e78c9d2ea9 95 // i2c.frequency(1000000);
mehatfie 0:20e78c9d2ea9 96 // lcd.setBacklight(TextLCD::LightOn);
mehatfie 0:20e78c9d2ea9 97 // wait(.6);
mehatfie 0:20e78c9d2ea9 98 // lcd.cls(); //clear the display
mehatfie 0:20e78c9d2ea9 99
mehatfie 0:20e78c9d2ea9 100 can.frequency(500000);
mehatfie 0:20e78c9d2ea9 101 can.attach(&receive, CAN::RxIrq);
mehatfie 0:20e78c9d2ea9 102
mehatfie 0:20e78c9d2ea9 103
mehatfie 0:20e78c9d2ea9 104 //********* Init the Thermocouple **********//
mehatfie 0:20e78c9d2ea9 105
mehatfie 0:20e78c9d2ea9 106 while (!device.initialize()); // keep calling until it works
mehatfie 0:20e78c9d2ea9 107
mehatfie 0:20e78c9d2ea9 108 device.setResolution(nineBit);
mehatfie 0:20e78c9d2ea9 109 }
mehatfie 0:20e78c9d2ea9 110
mehatfie 0:20e78c9d2ea9 111
mehatfie 0:20e78c9d2ea9 112 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 113 /************ waitSwitch *************/
mehatfie 0:20e78c9d2ea9 114 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 115
mehatfie 0:20e78c9d2ea9 116 void waitSwitch(char dataBytes[], int openTimeout){
mehatfie 0:20e78c9d2ea9 117
mehatfie 0:20e78c9d2ea9 118 int openComplete = 0;
mehatfie 0:20e78c9d2ea9 119 while (!openComplete){
mehatfie 0:20e78c9d2ea9 120
mehatfie 0:20e78c9d2ea9 121 openDoorCommand(dataBytes); //send CAN message to open the door
mehatfie 0:20e78c9d2ea9 122
mehatfie 0:20e78c9d2ea9 123 timer.reset();
mehatfie 0:20e78c9d2ea9 124 timer.start();
mehatfie 0:20e78c9d2ea9 125 int flag = 0;
mehatfie 0:20e78c9d2ea9 126 while (!flag){
mehatfie 0:20e78c9d2ea9 127 if (signal.read() == 0){
mehatfie 0:20e78c9d2ea9 128 while (!flag){
mehatfie 0:20e78c9d2ea9 129 debounceTimer.reset();
mehatfie 0:20e78c9d2ea9 130 debounceTimer.start();
mehatfie 0:20e78c9d2ea9 131 while (debounceTimer.read_ms() < 40);
mehatfie 0:20e78c9d2ea9 132 if ( signal.read() == 0){
mehatfie 0:20e78c9d2ea9 133 flag = 1;
mehatfie 0:20e78c9d2ea9 134 openComplete = 1;
mehatfie 0:20e78c9d2ea9 135 }
mehatfie 0:20e78c9d2ea9 136 }
mehatfie 0:20e78c9d2ea9 137 }
mehatfie 0:20e78c9d2ea9 138 else if (timer.read() >= openTimeout)
mehatfie 0:20e78c9d2ea9 139 flag = 1;
mehatfie 0:20e78c9d2ea9 140 }
mehatfie 0:20e78c9d2ea9 141
mehatfie 0:20e78c9d2ea9 142 timer.stop();
mehatfie 0:20e78c9d2ea9 143
mehatfie 0:20e78c9d2ea9 144 // timeout on opening
mehatfie 0:20e78c9d2ea9 145 if (timer.read() >= openTimeout){
mehatfie 0:20e78c9d2ea9 146
mehatfie 0:20e78c9d2ea9 147 avgCycTimer.stop(); //pause
mehatfie 0:20e78c9d2ea9 148
mehatfie 0:20e78c9d2ea9 149 dataBytes[3] = 0x01;
mehatfie 0:20e78c9d2ea9 150 can.write(CANMessage(534, dataBytes, 4)); // stop the door
mehatfie 0:20e78c9d2ea9 151 wait(0.1);
mehatfie 0:20e78c9d2ea9 152 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 153 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 154
mehatfie 0:20e78c9d2ea9 155 //Error Message
mehatfie 0:20e78c9d2ea9 156 // pc.setAddress ( 0, 1 );
mehatfie 0:20e78c9d2ea9 157 pc.printf( "<Press Button to Resume>\r\n" );
mehatfie 0:20e78c9d2ea9 158 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 159 pc.printf( "ERROR Open Timeout\r\n" );
mehatfie 0:20e78c9d2ea9 160
mehatfie 0:20e78c9d2ea9 161 while (!buttons.readSel()); //Do nothing until the button is selected
mehatfie 0:20e78c9d2ea9 162
mehatfie 0:20e78c9d2ea9 163 avgCycTimer.start(); //start
mehatfie 0:20e78c9d2ea9 164
mehatfie 0:20e78c9d2ea9 165 //Remove Error Message
mehatfie 0:20e78c9d2ea9 166 // pc.cls(); //clear the display
mehatfie 0:20e78c9d2ea9 167
mehatfie 0:20e78c9d2ea9 168 // pc.setAddress( 0, 0 );
mehatfie 0:20e78c9d2ea9 169 pc.printf( "Cycle %d of %3d\r\n", cycleCount, numCycles );
mehatfie 0:20e78c9d2ea9 170 // pc.setAddress( 0, 1 );
mehatfie 0:20e78c9d2ea9 171 pc.printf( "Avg t(sec): %1.3f\r\n", (totaltime / cycleCount));
mehatfie 0:20e78c9d2ea9 172 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 173 pc.printf( "STATUS: OPENING\r\n");
mehatfie 0:20e78c9d2ea9 174 }
mehatfie 0:20e78c9d2ea9 175 }
mehatfie 0:20e78c9d2ea9 176 }
mehatfie 0:20e78c9d2ea9 177
mehatfie 0:20e78c9d2ea9 178
mehatfie 0:20e78c9d2ea9 179 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 180 /************ Close Door / Switch Release *************/
mehatfie 0:20e78c9d2ea9 181 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 182
mehatfie 0:20e78c9d2ea9 183 void waitSwitchRelease(char dataBytes[], int switchReleaseTimeout){
mehatfie 0:20e78c9d2ea9 184
mehatfie 0:20e78c9d2ea9 185 int switchReleaseComplete = 0;
mehatfie 0:20e78c9d2ea9 186 while (!switchReleaseComplete){
mehatfie 0:20e78c9d2ea9 187
mehatfie 0:20e78c9d2ea9 188 closeDoorCommand(dataBytes); //send CAN message to close the door
mehatfie 0:20e78c9d2ea9 189
mehatfie 0:20e78c9d2ea9 190 timer.reset();
mehatfie 0:20e78c9d2ea9 191 timer.start();
mehatfie 0:20e78c9d2ea9 192 int flag = 0;
mehatfie 0:20e78c9d2ea9 193 while (!flag){
mehatfie 0:20e78c9d2ea9 194 if (signal.read() == 1){
mehatfie 0:20e78c9d2ea9 195 while (!flag){
mehatfie 0:20e78c9d2ea9 196 debounceTimer.reset();
mehatfie 0:20e78c9d2ea9 197 debounceTimer.start();
mehatfie 0:20e78c9d2ea9 198 while (debounceTimer.read_ms() < 40);
mehatfie 0:20e78c9d2ea9 199 if ( signal.read() == 1){
mehatfie 0:20e78c9d2ea9 200 flag = 1;
mehatfie 0:20e78c9d2ea9 201 switchReleaseComplete = 1;
mehatfie 0:20e78c9d2ea9 202 }
mehatfie 0:20e78c9d2ea9 203 }
mehatfie 0:20e78c9d2ea9 204 }
mehatfie 0:20e78c9d2ea9 205 else if (timer.read() >= switchReleaseTimeout)
mehatfie 0:20e78c9d2ea9 206 flag = 1;
mehatfie 0:20e78c9d2ea9 207 }
mehatfie 0:20e78c9d2ea9 208
mehatfie 0:20e78c9d2ea9 209 timer.stop();
mehatfie 0:20e78c9d2ea9 210
mehatfie 0:20e78c9d2ea9 211 // timeout on switch release
mehatfie 0:20e78c9d2ea9 212 if (timer.read() >= switchReleaseTimeout){
mehatfie 0:20e78c9d2ea9 213
mehatfie 0:20e78c9d2ea9 214 avgCycTimer.stop(); //pause
mehatfie 0:20e78c9d2ea9 215
mehatfie 0:20e78c9d2ea9 216 dataBytes[3] = 0x01;
mehatfie 0:20e78c9d2ea9 217 can.write(CANMessage(534, dataBytes, 4)); // stop the door
mehatfie 0:20e78c9d2ea9 218 wait(0.1);
mehatfie 0:20e78c9d2ea9 219 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 220 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 221
mehatfie 0:20e78c9d2ea9 222 //Error Message
mehatfie 0:20e78c9d2ea9 223 // pc.setAddress ( 0, 1 );
mehatfie 0:20e78c9d2ea9 224 pc.printf( "<Press Sel to Resume>\r\n" );
mehatfie 0:20e78c9d2ea9 225 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 226 pc.printf( "ERROR Release Switch Timeout\r\n" );
mehatfie 0:20e78c9d2ea9 227
mehatfie 0:20e78c9d2ea9 228 while (!buttons.readSel()); //Do nothing until the button is selected
mehatfie 0:20e78c9d2ea9 229
mehatfie 0:20e78c9d2ea9 230 avgCycTimer.start(); //start
mehatfie 0:20e78c9d2ea9 231
mehatfie 0:20e78c9d2ea9 232 //Remove Error Message
mehatfie 0:20e78c9d2ea9 233 // pc.cls(); //clear the display
mehatfie 0:20e78c9d2ea9 234
mehatfie 0:20e78c9d2ea9 235 // pc.setAddress( 0, 0 );
mehatfie 0:20e78c9d2ea9 236 pc.printf( "Cycle %d of %3d\r\n", cycleCount, numCycles );
mehatfie 0:20e78c9d2ea9 237 // pc.setAddress( 0, 1 );
mehatfie 0:20e78c9d2ea9 238 pc.printf( "Avg t(sec): %1.3f\r\n", (totaltime / cycleCount));
mehatfie 0:20e78c9d2ea9 239 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 240 pc.printf( "STATUS: SWITCH\r\n");
mehatfie 0:20e78c9d2ea9 241 }
mehatfie 0:20e78c9d2ea9 242 }
mehatfie 0:20e78c9d2ea9 243 }
mehatfie 0:20e78c9d2ea9 244
mehatfie 0:20e78c9d2ea9 245
mehatfie 0:20e78c9d2ea9 246 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 247 /************ Close Door to Latch *************/
mehatfie 0:20e78c9d2ea9 248 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 249
mehatfie 0:20e78c9d2ea9 250 void waitLatch(char dataBytes[], int closeTimeout){
mehatfie 0:20e78c9d2ea9 251
mehatfie 0:20e78c9d2ea9 252 //Loop through the close sequence until the latch is closed
mehatfie 0:20e78c9d2ea9 253 int closeComplete = 0;
mehatfie 0:20e78c9d2ea9 254 while (!closeComplete){
mehatfie 0:20e78c9d2ea9 255
mehatfie 0:20e78c9d2ea9 256 closeDoorCommand(dataBytes); //send CAN message to close the door
mehatfie 0:20e78c9d2ea9 257
mehatfie 0:20e78c9d2ea9 258 timer.reset();
mehatfie 0:20e78c9d2ea9 259 timer.start();
mehatfie 0:20e78c9d2ea9 260 int flag = 0;
mehatfie 0:20e78c9d2ea9 261 while (!flag){
mehatfie 0:20e78c9d2ea9 262
mehatfie 0:20e78c9d2ea9 263 if (checkLatchStatus()){
mehatfie 0:20e78c9d2ea9 264 flag = 1;
mehatfie 0:20e78c9d2ea9 265 closeComplete = 1;
mehatfie 0:20e78c9d2ea9 266 }
mehatfie 0:20e78c9d2ea9 267
mehatfie 0:20e78c9d2ea9 268 //if the timer goes off
mehatfie 0:20e78c9d2ea9 269 else if (timer.read() >= closeTimeout)
mehatfie 0:20e78c9d2ea9 270 flag = 1;
mehatfie 0:20e78c9d2ea9 271 }
mehatfie 0:20e78c9d2ea9 272
mehatfie 0:20e78c9d2ea9 273 timer.stop();
mehatfie 0:20e78c9d2ea9 274
mehatfie 0:20e78c9d2ea9 275 // timeout on closing
mehatfie 0:20e78c9d2ea9 276 if (timer.read() >= closeTimeout){
mehatfie 0:20e78c9d2ea9 277
mehatfie 0:20e78c9d2ea9 278 avgCycTimer.stop(); //pause
mehatfie 0:20e78c9d2ea9 279
mehatfie 0:20e78c9d2ea9 280 //Error Message
mehatfie 0:20e78c9d2ea9 281 // pc.setAddress ( 0, 1 );
mehatfie 0:20e78c9d2ea9 282 pc.printf( "<Press Sel to Resume>\r\n" );
mehatfie 0:20e78c9d2ea9 283 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 284 pc.printf( "ERROR Close Timeout\r\n" );
mehatfie 0:20e78c9d2ea9 285
mehatfie 0:20e78c9d2ea9 286 dataBytes[3] = 0x01;
mehatfie 0:20e78c9d2ea9 287 can.write(CANMessage(534, dataBytes, 4)); // stop the door
mehatfie 0:20e78c9d2ea9 288 wait(0.1);
mehatfie 0:20e78c9d2ea9 289 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 290 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 291
mehatfie 0:20e78c9d2ea9 292 while (!buttons.readSel()); //Do nothing until the button is selected
mehatfie 0:20e78c9d2ea9 293
mehatfie 0:20e78c9d2ea9 294 avgCycTimer.start(); //start
mehatfie 0:20e78c9d2ea9 295
mehatfie 0:20e78c9d2ea9 296 //Remove Error Message
mehatfie 0:20e78c9d2ea9 297 // pc.cls(); //clear the display
mehatfie 0:20e78c9d2ea9 298
mehatfie 0:20e78c9d2ea9 299 // pc.setAddress( 0, 0 );
mehatfie 0:20e78c9d2ea9 300 pc.printf( "Cycle %d of %3d\r\n", cycleCount, numCycles );
mehatfie 0:20e78c9d2ea9 301 // pc.setAddress( 0, 1 );
mehatfie 0:20e78c9d2ea9 302 pc.printf( "Avg t(sec): %1.3f\r\n", (totaltime / cycleCount));
mehatfie 0:20e78c9d2ea9 303 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 304 pc.printf( "STATUS: CLOSING\r\n");
mehatfie 0:20e78c9d2ea9 305 }
mehatfie 0:20e78c9d2ea9 306 }
mehatfie 0:20e78c9d2ea9 307 }
mehatfie 0:20e78c9d2ea9 308
mehatfie 0:20e78c9d2ea9 309
mehatfie 0:20e78c9d2ea9 310 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 311 /************ Check Latch Status *************/
mehatfie 0:20e78c9d2ea9 312 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 313
mehatfie 0:20e78c9d2ea9 314 // Returns 1 if latch is closed, 0 if it is not closed
mehatfie 0:20e78c9d2ea9 315 int checkLatchStatus(){
mehatfie 0:20e78c9d2ea9 316
mehatfie 0:20e78c9d2ea9 317 //convert the data array into a single 64 bit value so that we can grab the range of bits we desire, no matter where they're located
mehatfie 0:20e78c9d2ea9 318 long long allData = 0;
mehatfie 0:20e78c9d2ea9 319 for (int j = 0; j < 8; j++){
mehatfie 0:20e78c9d2ea9 320 long long tempData1 = (long long)readBuffer.data[j];
mehatfie 0:20e78c9d2ea9 321 tempData1 <<= (8 * j); //shift data bits into there proper position in the 64 bit long long
mehatfie 0:20e78c9d2ea9 322 allData |= tempData1;
mehatfie 0:20e78c9d2ea9 323 }
mehatfie 0:20e78c9d2ea9 324
mehatfie 0:20e78c9d2ea9 325 int _startBitValue = 4, _numBitsValue = 4; //get the latch status bits
mehatfie 0:20e78c9d2ea9 326 //isolate the desired range of bits for comparison
mehatfie 0:20e78c9d2ea9 327 // (_numBitsValue - 1) makes it so the following, startBit = 13, numBites = 5 would mean you want up to bit 17, but (13 + 5) brings you to 18, therefore do (numBits - 1)
mehatfie 0:20e78c9d2ea9 328 int compareBits = (allData >> _startBitValue) & ~(~0 << ((_startBitValue+(_numBitsValue-1))-_startBitValue+1));
mehatfie 0:20e78c9d2ea9 329
mehatfie 0:20e78c9d2ea9 330 int compareValue = 0; //value to watch (i.e. 0 for latch closed)
mehatfie 0:20e78c9d2ea9 331
mehatfie 0:20e78c9d2ea9 332 //if the latch is closed
mehatfie 0:20e78c9d2ea9 333 if (compareBits == compareValue)
mehatfie 0:20e78c9d2ea9 334 return 1;
mehatfie 0:20e78c9d2ea9 335
mehatfie 0:20e78c9d2ea9 336 return 0;
mehatfie 0:20e78c9d2ea9 337 }
mehatfie 0:20e78c9d2ea9 338
mehatfie 0:20e78c9d2ea9 339
mehatfie 0:20e78c9d2ea9 340 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 341 /************ getTemp *************/
mehatfie 0:20e78c9d2ea9 342 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 343
mehatfie 0:20e78c9d2ea9 344 float getTemp(){
mehatfie 0:20e78c9d2ea9 345
mehatfie 0:20e78c9d2ea9 346 return device.readTemperature();
mehatfie 0:20e78c9d2ea9 347 }
mehatfie 0:20e78c9d2ea9 348
mehatfie 0:20e78c9d2ea9 349 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 350 /************ Main *************/
mehatfie 0:20e78c9d2ea9 351 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 352
mehatfie 0:20e78c9d2ea9 353 int main() {
mehatfie 0:20e78c9d2ea9 354
mehatfie 0:20e78c9d2ea9 355 fullInit();
mehatfie 0:20e78c9d2ea9 356
mehatfie 0:20e78c9d2ea9 357 int openTimeout = 10; //10sec timeout
mehatfie 0:20e78c9d2ea9 358 int switchReleaseTimeout = 2;
mehatfie 0:20e78c9d2ea9 359 int closeTimeout = 10;
mehatfie 0:20e78c9d2ea9 360
mehatfie 0:20e78c9d2ea9 361 char dataBytes[4];
mehatfie 0:20e78c9d2ea9 362 dataBytes[0] = 0x00;
mehatfie 0:20e78c9d2ea9 363 dataBytes[1] = 0x00;
mehatfie 0:20e78c9d2ea9 364 dataBytes[2] = 0x00;
mehatfie 0:20e78c9d2ea9 365
mehatfie 0:20e78c9d2ea9 366
mehatfie 0:20e78c9d2ea9 367 // pc.setAddress ( 0, 3 );
mehatfie 0:20e78c9d2ea9 368 pc.printf( "<Press Button to start>\r\n" );
mehatfie 0:20e78c9d2ea9 369
mehatfie 0:20e78c9d2ea9 370
mehatfie 0:20e78c9d2ea9 371 // while (!buttons.readSel()){
mehatfie 0:20e78c9d2ea9 372 // if(buttons.readUp() && numCycles < 999999 ){
mehatfie 0:20e78c9d2ea9 373 // numCycles = numCycles + 100;
mehatfie 0:20e78c9d2ea9 374 // wait(0.2);
mehatfie 0:20e78c9d2ea9 375 // }
mehatfie 0:20e78c9d2ea9 376 // else if (buttons.readDown() && numCycles > 0 ){
mehatfie 0:20e78c9d2ea9 377 // numCycles = numCycles - 100;
mehatfie 0:20e78c9d2ea9 378 // wait(0.2);
mehatfie 0:20e78c9d2ea9 379 // }
mehatfie 0:20e78c9d2ea9 380 // pc.setAddress ( 0, 0 );
mehatfie 0:20e78c9d2ea9 381 // pc.printf( "<Num Cycles:%5d >" , numCycles );
mehatfie 0:20e78c9d2ea9 382 // }
mehatfie 0:20e78c9d2ea9 383
mehatfie 0:20e78c9d2ea9 384 // pc.cls(); //clear the display
mehatfie 0:20e78c9d2ea9 385
mehatfie 0:20e78c9d2ea9 386 // while(1){
mehatfie 0:20e78c9d2ea9 387 // currTemp = getTemp();
mehatfie 0:20e78c9d2ea9 388 // printf("%f",currTemp);
mehatfie 0:20e78c9d2ea9 389 // wait(1);
mehatfie 0:20e78c9d2ea9 390 // }
mehatfie 0:20e78c9d2ea9 391
mehatfie 0:20e78c9d2ea9 392 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 393 /************ Cycle Loop *************/
mehatfie 0:20e78c9d2ea9 394 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 395
mehatfie 0:20e78c9d2ea9 396 while (cycleCount <= numCycles){
mehatfie 0:20e78c9d2ea9 397
mehatfie 0:20e78c9d2ea9 398 // pc.setAddress( 0, 0 );
mehatfie 0:20e78c9d2ea9 399 pc.printf( "Cycle %d of %3d\r\n", cycleCount, numCycles );
mehatfie 0:20e78c9d2ea9 400 avgCycTimer.reset();
mehatfie 0:20e78c9d2ea9 401 avgCycTimer.start();
mehatfie 0:20e78c9d2ea9 402
mehatfie 0:20e78c9d2ea9 403
mehatfie 0:20e78c9d2ea9 404 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 405 /************ Open *************/
mehatfie 0:20e78c9d2ea9 406 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 407
mehatfie 0:20e78c9d2ea9 408 openDoorCommand(dataBytes);
mehatfie 0:20e78c9d2ea9 409
mehatfie 0:20e78c9d2ea9 410 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 411 pc.printf( "STATUS: OPENING\r\n");
mehatfie 0:20e78c9d2ea9 412
mehatfie 0:20e78c9d2ea9 413 waitSwitch(dataBytes, openTimeout);
mehatfie 0:20e78c9d2ea9 414
mehatfie 0:20e78c9d2ea9 415 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 416 /************ waitSwitchRelease *************/
mehatfie 0:20e78c9d2ea9 417 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 418
mehatfie 0:20e78c9d2ea9 419 closeDoorCommand(dataBytes);
mehatfie 0:20e78c9d2ea9 420
mehatfie 0:20e78c9d2ea9 421 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 422 pc.printf( "STATUS: SWITCH\r\n");
mehatfie 0:20e78c9d2ea9 423
mehatfie 0:20e78c9d2ea9 424 waitSwitchRelease(dataBytes, switchReleaseTimeout);
mehatfie 0:20e78c9d2ea9 425
mehatfie 0:20e78c9d2ea9 426 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 427 /************ waitSwitchRelease *************/
mehatfie 0:20e78c9d2ea9 428 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 429
mehatfie 0:20e78c9d2ea9 430 // pc.setAddress( 0, 2 );
mehatfie 0:20e78c9d2ea9 431 pc.printf( "STATUS: CLOSING\r\n");
mehatfie 0:20e78c9d2ea9 432
mehatfie 0:20e78c9d2ea9 433 waitLatch(dataBytes, closeTimeout);
mehatfie 0:20e78c9d2ea9 434
mehatfie 0:20e78c9d2ea9 435 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 436 /************ End Cycle *************/
mehatfie 0:20e78c9d2ea9 437 /******************************************************************************/
mehatfie 0:20e78c9d2ea9 438
mehatfie 0:20e78c9d2ea9 439 totaltime += avgCycTimer.read();
mehatfie 0:20e78c9d2ea9 440 // pc.setAddress( 0, 1 );
mehatfie 0:20e78c9d2ea9 441 pc.printf( "Avg t(sec): %1.3f\r\n", (totaltime / cycleCount));
mehatfie 0:20e78c9d2ea9 442 wait(0.2);
mehatfie 0:20e78c9d2ea9 443 cycleCount++;
mehatfie 0:20e78c9d2ea9 444 }
mehatfie 0:20e78c9d2ea9 445 }
mehatfie 0:20e78c9d2ea9 446
mehatfie 0:20e78c9d2ea9 447
mehatfie 0:20e78c9d2ea9 448
mehatfie 0:20e78c9d2ea9 449
mehatfie 0:20e78c9d2ea9 450
mehatfie 0:20e78c9d2ea9 451
mehatfie 0:20e78c9d2ea9 452
mehatfie 0:20e78c9d2ea9 453
mehatfie 0:20e78c9d2ea9 454
mehatfie 0:20e78c9d2ea9 455
mehatfie 0:20e78c9d2ea9 456
mehatfie 0:20e78c9d2ea9 457
mehatfie 0:20e78c9d2ea9 458
mehatfie 0:20e78c9d2ea9 459
mehatfie 0:20e78c9d2ea9 460 /*
mehatfie 0:20e78c9d2ea9 461 pc.setAddress ( 0, 0 );
mehatfie 0:20e78c9d2ea9 462 pc.printf( "^ Up = Open" );
mehatfie 0:20e78c9d2ea9 463 pc.setAddress ( 0, 1 );
mehatfie 0:20e78c9d2ea9 464 pc.printf( "v Down = Close" );
mehatfie 0:20e78c9d2ea9 465 pc.setAddress ( 0, 2 );
mehatfie 0:20e78c9d2ea9 466 pc.printf( "> Right = Stop" );
mehatfie 0:20e78c9d2ea9 467
mehatfie 0:20e78c9d2ea9 468 while(1){
mehatfie 0:20e78c9d2ea9 469 if (buttons.readUp()){
mehatfie 0:20e78c9d2ea9 470 dataBytes[3] = 0x03;
mehatfie 0:20e78c9d2ea9 471 can.write(CANMessage(534, dataBytes, 4)); // open the door
mehatfie 0:20e78c9d2ea9 472 wait(0.1);
mehatfie 0:20e78c9d2ea9 473 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 474 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 475 }
mehatfie 0:20e78c9d2ea9 476 else if (buttons.readDown()){
mehatfie 0:20e78c9d2ea9 477 dataBytes[3] = 0x02;
mehatfie 0:20e78c9d2ea9 478 can.write(CANMessage(534, dataBytes, 4)); // close the door
mehatfie 0:20e78c9d2ea9 479 wait(0.1);
mehatfie 0:20e78c9d2ea9 480 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 481 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 482 }
mehatfie 0:20e78c9d2ea9 483 else if (buttons.readRight()){
mehatfie 0:20e78c9d2ea9 484 dataBytes[3] = 0x01;
mehatfie 0:20e78c9d2ea9 485 can.write(CANMessage(534, dataBytes, 4)); // stop the door
mehatfie 0:20e78c9d2ea9 486 wait(0.1);
mehatfie 0:20e78c9d2ea9 487 dataBytes[3] = 0x00;
mehatfie 0:20e78c9d2ea9 488 can.write(CANMessage(534, dataBytes, 4)); // Set to IDLE
mehatfie 0:20e78c9d2ea9 489 }
mehatfie 0:20e78c9d2ea9 490 }*/
mehatfie 0:20e78c9d2ea9 491 /*
mehatfie 0:20e78c9d2ea9 492 while (1){
mehatfie 0:20e78c9d2ea9 493 //convert the data array into a single 64 bit value so that we can grab the range of bits we desire, no matter where they're located
mehatfie 0:20e78c9d2ea9 494 long long allData = 0; //((long long)*readMsg[k].data);
mehatfie 0:20e78c9d2ea9 495 for (int j = 0; j < 8; j++){
mehatfie 0:20e78c9d2ea9 496 long long tempData1 = (long long)readBuffer.data[j];
mehatfie 0:20e78c9d2ea9 497 tempData1 <<= (8 * j); //shift data bits into there proper position in the 64 bit long long
mehatfie 0:20e78c9d2ea9 498 allData |= tempData1;
mehatfie 0:20e78c9d2ea9 499 }
mehatfie 0:20e78c9d2ea9 500
mehatfie 0:20e78c9d2ea9 501 int _startBitValue = 4, _numBitsValue = 4; //get the latch status bits
mehatfie 0:20e78c9d2ea9 502 //isolate the desired range of bits for comparison
mehatfie 0:20e78c9d2ea9 503 // (_numBitsValue - 1) makes it so the following, startBit = 13, numBites = 5 would mean you want up to bit 17, but (13 + 5) brings you to 18, therefore do (numBits - 1)
mehatfie 0:20e78c9d2ea9 504 int compareBits = (allData >> _startBitValue) & ~(~0 << ((_startBitValue+(_numBitsValue-1))-_startBitValue+1));
mehatfie 0:20e78c9d2ea9 505
mehatfie 0:20e78c9d2ea9 506 int compareValue = 0; //value to watch (i.e. 0 for latch closed)
mehatfie 0:20e78c9d2ea9 507 //if the latch is closed
mehatfie 0:20e78c9d2ea9 508 if (compareBits == compareValue){
mehatfie 0:20e78c9d2ea9 509 pc.setAddress ( 0, 0 );
mehatfie 0:20e78c9d2ea9 510 pc.printf( "Latch: Closed " );
mehatfie 0:20e78c9d2ea9 511 }
mehatfie 0:20e78c9d2ea9 512 else{
mehatfie 0:20e78c9d2ea9 513 pc.setAddress ( 0, 0 );
mehatfie 0:20e78c9d2ea9 514 pc.printf( "Latch: Not Closed " );
mehatfie 0:20e78c9d2ea9 515 }
mehatfie 0:20e78c9d2ea9 516 }*/