
Slams doors
Dependencies: Motor RemoteIR TextLCD mbed
Revision 0:15e49f97cb3d, committed 2012-10-12
- Comitter:
- mversteeg3
- Date:
- Fri Oct 12 17:49:16 2012 +0000
- Commit message:
- Door slammer;
Changed in this revision
diff -r 000000000000 -r 15e49f97cb3d Motor.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.lib Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Motor/#f265e441bcd9
diff -r 000000000000 -r 15e49f97cb3d RemoteIR.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RemoteIR.lib Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/RemoteIR/#268cc2ab63bd
diff -r 000000000000 -r 15e49f97cb3d TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r 15e49f97cb3d define.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/define.h Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,71 @@ +#include "globlvar.h" + +//Put all variables that must be accessible to all functions here. + +#ifndef DEFINE_H +#define DEFINE_H + +#define PRINT(message) pc.printf( (message) ); + +#define PRINTLN(message) pc.printf( (message) );\ + pc.printf("\n\r"); + +#define LED(i) leds[i] + +#define LED_0 1 +#define LED_1 2 +#define LED_2 4 +#define LED_3 8 + +#define CODE_ON +#define FORWARD 1 +#define BACKWARD -1 + + +//OPTIONS + +//Which side of the arm is the door +#define DIRECTION BACKWARD +//Minimum force to start pushing +#define FORCE_MIN .05 +//Default value of the force threshold +#define DEFAULT_FORCE .4 +//Default value for the speed +#define DEFAULT_SPEED 1 +//Speed of the arm when not in contact with door +#define COAST_SPEED .3 +//Speed of the arm when returning to wall +#define REVERSE_SPEED -0.4 +//Force threshold for contact with wall +#define WALL_THRESH .05 +//How long to push after contacting door +#define PUSH_TIME .5 +//How much the speed changes with each button press +#define SPEED_INC 0.05f +//How much the force changes with each button press +#define FORCE_INC 0.1f + + + +#define SPEED_MIN COAST_SPEED + +//BUTTON DEFINITIONS +#define NONE -1 +#define SPD_DN 2 +#define SPD_UP 3 +#define FRC_UP 0 +#define FRC_DN 1 +#define RUN 14 +#define RST 69 + + + +#define SET_LEDS(mask, state) LED(0) = ((mask)&1!=0); \ + LED(1) = ((mask)&2!=0); \ + LED(2) = ((mask)&4!=0); \ + LED(0) = ((mask)&8!=0); +#define ON 1 +#define OFF 0 + + +#endif \ No newline at end of file
diff -r 000000000000 -r 15e49f97cb3d func.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/func.h Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,57 @@ +#include "globlvar.h" +#ifndef FUNC_H +#define FUNC_H + +//Function that checks the IR Receiver for a message, returning the first byte of the array, +//which is a unioque between the buttons used by this device. +int IRListen() +{ + uint8_t buf[32]; + int bitcount = 0; + if (ir_rx.getState() == ReceiverIR::Received) { + bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8); + } else { + return -1; + } + if(bitcount > 0) { + //for(int i = 0; i < bitcount; i++) { + pc.printf("%d ", buf[0]); + //} + pc.printf("\n\r"); + return buf[0]; + } + return -1; + +} +//Debugging application used to get the IR codes for each button press. +void GetIRCodes() +{ + while(1) { + uint8_t buf[32]; + int bitcount = 0; + if (ir_rx.getState() == ReceiverIR::Received) { + bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8); + if(bitcount > 0) { + pc.printf("Bitcount: %d, sizeof(buf): %d\n\r", bitcount, sizeof(buf)*8); + for(int i = 0; i < 32; i++) { + pc.printf("%d ", buf[i]); + } + pc.printf("\n\r"); + } + } + wait(.1); + } +} +//Debugging function that outputs the forces on each sensor. +void GetForces() +{ + while(1) + { + force = doorForce; + wall = wallForce; + lcd.printf("Door: %f\nWall: %f", force, wall); + wait(.3); + } +} + +#endif \ No newline at end of file
diff -r 000000000000 -r 15e49f97cb3d globlvar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globlvar.h Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#include "ReceiverIR.h" +#include "Motor.h" +#include "TextLCD.h" + +#ifndef GLOBLVAR_H +#define GLOBLVAR_H + +//The IR receiver +ReceiverIR ir_rx(p15); +//The output serial port +Serial pc(USBTX, USBRX); +DigitalOut leds[] = {(LED1), (LED2),(LED3), (LED4)}; +//The force sensors +AnalogIn doorForce(p16); +AnalogIn wallForce(p20); +//The motor +Motor myMotor(p23,p6,p5); +//The LCD +TextLCD lcd(p24, p25, p26, p27, p28, p29, TextLCD::LCD20x4); // rs, e, d4-d7 + +bool newState=false; +bool held; +bool signal; +float force; +float wall; +float speed; +int speedPercent; + +RemoteIR::Format format; +float pushSpeed; +float forceThresh; + +//The states +enum RunState +{ + BOOT = 0, + LISTEN, + CLOSE_DOOR, + CHECK_IR, + CHECK_FORCE, + CHECK_WALL, + RUN_MOTOR, + RESET, +}; + + +RunState state = BOOT; + +#endif \ No newline at end of file
diff -r 000000000000 -r 15e49f97cb3d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,151 @@ +#include "mbed.h" +#include "func.h" +#include "define.h" +#include "globlvar.h" + + +int main() +{ + int myButton; + //For developer use + //GetIRCodes(); + //GetForces(); + format = RemoteIR::SONY; + while(1) { + //The state machine switch statement + switch( state) { + //The initialization state + case BOOT: + pc.printf("BOOTING UP\n\r"); + forceThresh = DEFAULT_FORCE; + pushSpeed = DEFAULT_SPEED; + state = LISTEN; + newState = true; + held = false; + lcd.cls(); + lcd.printf("RON SWANSON \nDOOR SLAMMER!"); + break; + //Listen for IR signal + case LISTEN: + if(newState) + pc.printf("LISTENING FOR IR\n\r"); + newState = false; + myButton = IRListen(); + + switch(myButton) { + case NONE: + break; + case RUN: + pc.printf("OK Button\n\r"); + state = CLOSE_DOOR; + newState = true; + held = true; + break; + case SPD_UP: + if(pushSpeed+SPEED_INC<=1) + pushSpeed += SPEED_INC; + else + pushSpeed = 1; + speedPercent = (int)(pushSpeed*100.0f); + lcd.cls(); + lcd.printf("Speed:\n%3d%%\n\r", speedPercent); + pc.printf("Speed:\n%3d%%\n\r", speedPercent); + break; + case SPD_DN: + if(pushSpeed-SPEED_INC>=SPEED_MIN) + pushSpeed -= SPEED_INC; + else + pushSpeed = SPEED_MIN; + speedPercent = (int)(pushSpeed*100.0f); + lcd.cls(); + lcd.printf("Speed:\n%3d%%\n\r", speedPercent); + pc.printf("Speed:\n%3d%%\n\r", speedPercent); + break; + case RST: + pc.printf("Exit Button\n\r"); + state = BOOT; + break; + case FRC_UP: + + if(forceThresh+FORCE_INC<=1) + forceThresh+=FORCE_INC; + lcd.cls(); + lcd.printf("Force threshold:\n%f pounds\n", (forceThresh*200.0f)/4.44822162f); + pc.printf("Force threshold:\n\r%f pounds\n\r", (forceThresh*200.0f)/4.44822162f); + break; + case FRC_DN: + if(forceThresh-FORCE_INC>0) + forceThresh-=FORCE_INC; + lcd.cls(); + lcd.printf("Force threshold:\n%f pounds\n", (forceThresh*200.0f)/4.44822162f); + pc.printf("Force threshold:\n\r%f pounds\n\r", (forceThresh*200.0f)/4.44822162f); + break; + default: + pc.printf("Unknown Button: %d\n\r", myButton); + break; + } + break; + case CLOSE_DOOR: + lcd.cls(); + lcd.printf("GET OUT\n"); + pc.printf("CLOSING DOOR\n\r"); + state = CHECK_IR; + break; + case CHECK_IR: + pc.printf("CHECKING FOR CANCEL SIGNAL\n\r"); + if(IRListen()==-1) + held = false; + else if(!held) { + pc.printf("CANCEL SIGNAL RECEIVED\n\r"); + state = RESET; + break; + } + state = CHECK_FORCE; + break; + case CHECK_FORCE: + force = doorForce; + pc.printf("CHECKING FORCE: %f\n\r", force); + if(force<FORCE_MIN) + speed = COAST_SPEED; + else if(force<forceThresh) + speed = pushSpeed; + else { + wait(PUSH_TIME); + state = RESET; + break; + } + state = RUN_MOTOR; + pc.printf("\tNEW SPEED: %f\n\r", speed); + break; + case RUN_MOTOR: + myMotor.speed(DIRECTION*speed); + + state = CHECK_IR; + break; + case RESET: + pc.printf("RETURNING TO IDLE\n\r"); + myMotor.speed(0); + myMotor.speed(DIRECTION*REVERSE_SPEED); + state = CHECK_WALL; + break; + case CHECK_WALL: + wall = wallForce; + if(wall>=WALL_THRESH||IRListen()>=0) { + myMotor.speed(0); + state = LISTEN; + lcd.printf("AND STAY OUT!"); + pc.printf("WALL DETECTED, ENTERING IDLE STATE\n\r"); + + break; + } + pc.printf("CHECKING WALL: %f\n\r", wall); + + break; + default: + break; + } + + + } +} +
diff -r 000000000000 -r 15e49f97cb3d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Oct 12 17:49:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc \ No newline at end of file