GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002 * EECS 397
00003 *
00004 * Assignment Name: Lab 6: WaG
00005 * 
00006 * Authors: Sam Morrison and Phong Nguyen 
00007 * File name: main.cpp
00008 * Purpose: Configures the dispaly for 4-digit display or motor control
00009 *
00010 * Created: 03/01/2018
00011 * Last Modified: 04/06/2018
00012 *
00013 ******************************************************************************/
00014 #include "mbed.h"
00015 #include "io_pins.h"
00016 #include "display.h"
00017 #include "spi.h"
00018 #include "stepper.h"
00019 #include "utility.h"
00020 #include "analog.h"
00021 #include "laser.h"
00022 #include "wag.h"
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 SPI wag_spi(MOSI, MISO, SCLK);
00028 Serial pc(USBTX, USBRX);
00029 DigitalIn jog_cw(UI_JOG_RIGHT_BUTTON);
00030 DigitalIn jog_ccw(UI_JOG_LEFT_BUTTON);
00031 DigitalIn my_button(USER_BUTTON);
00032 DigitalIn start_button(UI_START_BUTTON);
00033 DigitalIn cal_button(UI_CAL_BUTTON);
00034 DigitalIn station_select(UI_STATION_SELECT);
00035 DigitalIn home_sensor(STP_HOME_SENSOR);
00036 DigitalOut laser(LZR_ENABLE);
00037 AnalogIn mux_out (MUX_OUT);
00038 BusOut mux_select(MUX_S0, MUX_S1, MUX_S2, MUX_S3);
00039 int station = -1;
00040 int cal_status = NOT_CALIBRATED;
00041 float sensor_values[TGT_SENSOR_QUAN * 2];
00042 int stp_sensor_pos[TGT_SENSOR_QUAN];
00043 
00044 struct spi_cfg as1107{
00045     SPI_AS1107_ID,
00046     DSP_AS1107_NCS,
00047     SPI_NO_ID,
00048     AS1107_SPI_FREQ,
00049     AS1107_SPI_NO_BITS,
00050 };
00051 
00052 
00053 int main(void) {
00054     initial_setup(as1107);
00055     
00056     // pull up UI_STATION_SELECT
00057     station_select.mode(PullUp);
00058     start_button.mode(PullUp);
00059     
00060     //set all digits to zero
00061     spi_send(as1107, 0x0100);
00062     spi_send(as1107, 0x0200);
00063     spi_send(as1107, 0x0300);
00064     spi_send(as1107, 0x0400);
00065 
00066     pc.printf("Press user button to calibrate.\n");
00067     
00068     // determine if the wag is connected to station A or station B
00069     station = station_select.read();
00070     pc.printf("station in main: %d\n", station);
00071     
00072     stp_init();
00073     
00074     //calibration test
00075     stp_calibrate(station, sensor_values, &cal_status);
00076     
00077     //something went wrong with calibration, quit the program
00078     if (cal_status == NOT_CALIBRATED) {
00079         pc.printf("Critical error: station not calibrated.");
00080         while(1);
00081     }
00082     
00083     // play order
00084     if (station == STATION_A) {
00085         while (uti_chk_ubutton() == 0);
00086         gnoll(0, sensor_values);
00087         while (uti_chk_ubutton() == 0);
00088         whack(0, sensor_values);
00089     }
00090     else if (station == STATION_B) {
00091         while (uti_chk_ubutton() == 0);
00092         whack(8, sensor_values);
00093         while (uti_chk_ubutton() == 0);
00094         gnoll(8, sensor_values);
00095     }
00096     else {
00097         pc.printf("Critical error: no station detected.");
00098     }
00099 }