//----------------------Funktioner------------------------ void init_LCD() { const char lcd_text[] = "ADC value:"; LCDinit(); _delay_ms(1000); LCDclr(); _delay_ms(1000); LCDhome(); _delay_ms(1000); LCDstring((uint8_t*) lcd_text, (uint8_t) strlen(lcd_text)); _delay_ms(1000); LCDvisible(); }
/* * Elevator car functionality * - */ void car(void) { byte ret; byte sw1_pressed = 0, sw2_pressed = 0; char *command, *floor, *direction; byte cur_floor; byte elevatorDirection = 0xFF; CANframe txframe; // Transmitted CAN frames byte rxmessage[PAYLOAD_SIZE]; // Received data payload cur_floor = 0; // Message to controller; floor 1 txframe.id = MSCAN_CTL_ID; txframe.priority = 0x01; txframe.length = 2; txframe.payload[0] = CMD_BUTTON_CAR; txframe.payload[2] = DIRECTION_STATIONARY; if(BTN_FLOOR1) { if (BUTTON_FL1 > cur_floor) { elevatorDirection = DIRECTION_DOWN; } else if (BUTTON_FL1 < cur_floor) { elevatorDirection = DIRECTION_UP; } else { elevatorDirection = DIRECTION_STATIONARY; } txframe.payload[1] = BUTTON_FL1; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } if(BTN_FLOOR2) { if (BUTTON_FL2 > cur_floor) { elevatorDirection = DIRECTION_DOWN; } else if (BUTTON_FL2 < cur_floor) { elevatorDirection = DIRECTION_UP; } else { elevatorDirection = DIRECTION_STATIONARY; } txframe.payload[1] = BUTTON_FL2; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } if(BTN_FLOOR3) { if (BUTTON_FL3 > cur_floor) { elevatorDirection = DIRECTION_DOWN; } else if (BUTTON_FL3 < cur_floor) { elevatorDirection = DIRECTION_UP; } else { elevatorDirection = DIRECTION_STATIONARY; } txframe.payload[1] = BUTTON_FL3; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } if(BTN_DROPEN) { txframe.payload[1] = BUTTON_DOOR_OPEN; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } if(BTN_DRCLOSE) { txframe.payload[1] = BUTTON_DOOR_CLOSE; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } if(BTN_ESTOP) { txframe.payload[1] = BUTTON_STOP; txframe.payload[2] = elevatorDirection; ret = CANsend(&txframe); if(ret) { // Message could not be sent! } } // CAN bus <-> serial link // Check for new incoming messages and send out received messages via serial runSerialCAN(MSCAN_NODE_ID); if(data_available()) { CANget(rxmessage); switch(rxmessage[0]) { case CMD_LOCATION: command = "Loc"; break; case CMD_BUTTON_CALL: command = "Call"; break; case CMD_BUTTON_CAR: command = "Car"; break; case CMD_DISP_APPEND: command = "Disp"; break; case CMD_ERROR: command = "Err"; break; default: // Command didn't match known commands! goto car_cmd_error; } switch(rxmessage[1]) { case FLOOR1: floor = "1"; cur_floor = 1; break; case FLOOR2: floor = "2"; cur_floor = 2; break; case FLOOR3: floor = "3"; cur_floor = 3; break; default: // Command didn't match known commands! goto car_cmd_error; } // Turn off LED when car arrives at requested floor if ( rxmessage[0] == CMD_LOCATION ) { if( rxmessage[1] == FLOOR1 ) { LED1 = 0; } if( rxmessage[1] == FLOOR2 ) { LED2 = 0; } /*if( rxmessage[1] == FLOOR3 ) { LED3 = 0; }*/ } #ifdef USE_LED7 led7_write(led7_table[cur_floor]); #endif #ifdef USE_LCD LCDhome(); LCDclear(); LCDprintf("Command: %s\nFloor%s", command, floor); #endif return; car_cmd_error: //LCDhome(); //LCDclear(); //LCDprintf("Error in\ncommand"); return; } }
/* * Controller functionality * - Send elevator location messages to callboxes * - Listen for button press messages */ void controller() { byte sw1_pressed = 0, sw2_pressed = 0; byte rxmessage[PAYLOAD_SIZE]; // Received data payload byte button_pressed; byte next_floor; char *button_floor_str, *button_direction_str; byte update_lcd = 1; byte cycle_count = 0; word distance; // car height in cm, distance measurement in mm byte cur_floor; byte last_floor = 0; //byte b; // used for debug manual frame sending testing dist_init(); mctrl_init(); // Initialize servo motor controller for(;;) { mctrl_update(); cycle_count++; if ( cycle_count == 10 ) { update_lcd = 1; cycle_count = 0; } if ( update_lcd ) { update_lcd = 0; /* #ifdef USE_LCD if ( cur_floor == 0 ) { LCDclear(); LCDputs("No car"); } else { LCDclear(); LCDprintf("%dmm/F%d", car_height, cur_floor); } #endif */ } // CAN bus <-> serial link // Check for new incoming messages and send out received messages via serial runSerialCAN(MSCAN_NODE_ID); /* while ( sci_bytesAvailable() ) { sci_readByte(&b); lcd_putc(b); } */ if(data_available()) { CANget(rxmessage); switch(rxmessage[0]) { case CMD_BUTTON_CALL: button_pressed = rxmessage[1]; addToQueue(button_pressed); next_floor = peekNextFloor(); pid_setpoint(FLOOR2SETPOINT(next_floor)); switch(cur_floor) { case FLOOR1: button_floor_str = "1"; break; case FLOOR2: button_floor_str = "2"; break; case FLOOR3: button_floor_str = "3"; break; default: break; } if(next_floor == cur_floor) { button_direction_str = "stat"; } else if(next_floor > cur_floor) { button_direction_str = "up "; } else { button_direction_str = "down"; } #ifdef USE_LCD LCDhome(); LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str); #else #ifdef USE_LCD2 lcd_goto(0x10); // Start at second line lcd_puts("Floor"); lcd_puts(button_floor_str); lcd_puts(" Dir "); lcd_puts(button_direction_str); #endif #endif break; case CMD_BUTTON_CAR: button_pressed = rxmessage[1]; if(button_pressed == BUTTON_STOP) { // call emergency stop function } else if(button_pressed < BUTTON_DOOR_CLOSE) { addToQueue(button_pressed); } next_floor = peekNextFloor(); pid_setpoint(FLOOR2SETPOINT(next_floor)); switch(cur_floor) { case FLOOR1: button_floor_str = "1"; break; case FLOOR2: button_floor_str = "2"; break; case FLOOR3: button_floor_str = "3"; break; default: break; } if(next_floor == cur_floor){ button_direction_str = "stat"; }else if(next_floor > cur_floor){ button_direction_str = "up "; }else { button_direction_str = "down"; } #ifdef USE_LCD LCDhome(); LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str); #endif #ifdef USE_LCD2 lcd_goto(0x10); // Start at second line lcd_puts("Floor"); lcd_puts(button_floor_str); lcd_puts(" Dir "); lcd_puts(button_direction_str); #endif break; case CMD_DISTANCE: distance = (rxmessage[1] << 8) | rxmessage[2]; pid_feedback(distance); if (distance < SETPOINT_F1 + FLOOR_MARGIN) cur_floor = FLOOR1; if (distance > SETPOINT_F2 - FLOOR_MARGIN && distance < SETPOINT_F2 + FLOOR_MARGIN) cur_floor = FLOOR2; if (distance > SETPOINT_F3 - FLOOR_MARGIN && distance < SETPOINT_F3 + FLOOR_MARGIN) cur_floor = FLOOR3; if ( distance > 1500 ) { cur_floor = 0; #ifdef USE_LED7 led7_write(led7_bars[1]); #endif } else { #ifdef USE_LED7 led7_write(led7_table[cur_floor]); #endif if ( cur_floor != last_floor ) { update_floor(cur_floor); last_floor = cur_floor; // if we have reached the target floor, pop off the top of the queue // TODO: change name of getNextFloor() to be more descriptive if(cur_floor == next_floor){ getNextFloor(); next_floor = peekNextFloor(); pid_setpoint(FLOOR2SETPOINT(next_floor)); } } } #ifdef USE_LCD LCDhome(); LCDprintf("Dist: %4d", distance); #endif break; case CMD_ERROR: break; default: #ifdef USE_LCD LCDclear(); LCDputs("\nUnknown command"); #endif #ifdef USE_LCD2 lcd_goto(0x10); // Start at second line lcd_puts("Unknown command"); #endif break; } } delay_ms(100); } }
int main(void) { //var uint8_t answer; uint8_t http_respon_data[64]; //init pa sbg in DDRA = 0; PORTA = 0xff; //init lcd LCDinit(); LCDclr(); LCDcursorOFF(); LCDhome(); //init uart sim900_init_uart(38400); //enable isr sei(); LCDGotoXY(0, 0); fprintf(&LCDInputOutputStream, "setup modem = %d", sim900_setup(SETUP_WAIT_INFINITE)); _delay_ms(1000); while(1) { //krm sms pa0 if (bit_is_clear(PINA, PA0)) { _delay_ms(100); loop_until_bit_is_set(PINA, PA0); LCDclr(); LCDhome(); fprintf(&LCDInputOutputStream, "open gprs = %d", sim900_gprs_open_connection( (const uint8_t*)"internet", (const uint8_t*)" ", (const uint8_t*)" ")); } //open gprs if (bit_is_clear(PINA, PA1)) { _delay_ms(100); loop_until_bit_is_set(PINA, PA1); //answer = sim900_gprs_open_bearer((const uint8_t*)"internet", (const uint8_t*)" ", (const uint8_t*)" "); //answer = sim900_gprs_open_bearer("internet", " ", " "); //answer = sim900_gprs_is_opened(); //answer = sim900_gprs_closed(); //answer = sim900_http_get((const uint8_t*) //"http://ex4-tech.id.or.id/sim900/data.php", (const uint8_t*)"c=1", 64, //http_respon_data); answer = sim900_http_send_data( HTTP_POST, (const uint8_t*)"http://ex4-tech.id.or.id/sim900/data.php", (const uint8_t*)"c=1", 64, http_respon_data); LCDclr(); LCDhome(); fprintf(&LCDInputOutputStream, "post = %d", answer); LCDGotoXY(0, 1); fprintf(&LCDInputOutputStream, "%s", http_respon_data); _delay_ms(5000); } if (bit_is_clear(PINA, PA2)) { _delay_ms(100); loop_until_bit_is_set(PINA, PA2); answer = sim900_gprs_close_connection(); LCDclr(); LCDhome(); fprintf(&LCDInputOutputStream, "close gprs = %d", answer); } } }