dist_readings_t dist_read_all() { dist_readings_t readings; // Read raw values from sensors dist_read(&readings.front_val, &readings.side_1_val, &readings.side_2_val); // Convert the numbers to a distance in centimeters readings.front = get_dist_to_cm(readings.front_val); readings.side_1 = get_dist_to_cm(readings.side_1_val); readings.side_2 = get_dist_to_cm(readings.side_2_val); return readings; }
void callbox(byte my_floor) { byte rxmessage[PAYLOAD_SIZE]; // Received data payload static byte floor, direction; word distance; static byte flag_dist_init = 0; CANframe txframe; // Transmitted CAN frame floor = 0xFF; // Start at false floor direction = DIRECTION_STATIONARY; // Assume starting car direction is stationary if (!flag_dist_init){ dist_init(); flag_dist_init = 1; } if(SW1 && !sw1_pressed) { sw1_pressed = 1; button_up(my_floor); } if(SW2 && !sw2_pressed) { sw2_pressed = 1; button_down(my_floor); } if(!SW1) sw1_pressed = 0; if(!SW2) sw2_pressed = 0; runSerialCAN(MSCAN_NODE_ID); if(data_available()) { CANget(rxmessage); switch(rxmessage[0]) { case CMD_LOCATION: floor = rxmessage[1]; direction = rxmessage[2]; #ifdef USE_LCD LCDclear(); LCDprintf("Floor: %d\nDir: %d", floor, direction); #endif #ifdef USE_LED7 led7_write(led7_table[floor]); #endif break; case CMD_BUTTON_CALL: rxmessage[1] == DIRECTION_UP ? button_up(my_floor) : button_down(my_floor); break; case CMD_ERROR: #ifdef USE_LCD LCDclear(); LCDprintf("Error condition\nreceived!"); #endif break; default: #ifdef USE_LCD LCDclear(); LCDputs("Unknown command"); #endif break; } // Turn off indicator LED once car has reached local floor if(floor == my_floor) { LED1 = 0; LED2 = 0; } } // Sonar sensor currently attached to callbox 1 // Send off distance message to controller node if (my_floor == FLOOR1) { distance = dist_read(); txframe.id = MSCAN_CTL_ID; txframe.priority = 0x01; txframe.length = 3; txframe.payload[0] = CMD_DISTANCE; txframe.payload[1] = (distance & 0xFF00) >> 8; txframe.payload[2] = (distance & 0x00FF); CANsend(&txframe); }