Lab 6 code.

Dependencies:   mbed

Fork of WaG by GroupA

Committer:
spm71
Date:
Fri Apr 06 18:25:19 2018 +0000
Revision:
56:048b30c9f2a1
Parent:
55:1a25dd75e309
Final update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spm71 0:ee6e5c60dd2d 1 /******************************************************************************
spm71 0:ee6e5c60dd2d 2 * EECS 397
spm71 0:ee6e5c60dd2d 3 *
spm71 42:6cba679a4ee4 4 * Assignment Name: Lab 6: WaG
spm71 0:ee6e5c60dd2d 5 *
spm71 0:ee6e5c60dd2d 6 * Authors: Sam Morrison and Phong Nguyen
phn10 16:dfa9eb1a808d 7 * File name: main.cpp
spm71 22:09dd6977576b 8 * Purpose: Configures the dispaly for 4-digit display or motor control
spm71 0:ee6e5c60dd2d 9 *
spm71 18:0e281922212c 10 * Created: 03/01/2018
spm71 56:048b30c9f2a1 11 * Last Modified: 04/06/2018
spm71 0:ee6e5c60dd2d 12 *
spm71 0:ee6e5c60dd2d 13 ******************************************************************************/
spm71 0:ee6e5c60dd2d 14 #include "mbed.h"
phn10 8:d8bc78bda829 15 #include "io_pins.h"
phn10 8:d8bc78bda829 16 #include "display.h"
spm71 20:d23bcd97f2c5 17 #include "spi.h"
spm71 20:d23bcd97f2c5 18 #include "stepper.h"
spm71 23:3da1d39c1ae9 19 #include "utility.h"
spm71 38:57af77435ae1 20 #include "analog.h"
spm71 42:6cba679a4ee4 21 #include "laser.h"
phn10 44:4c2ba5bbba67 22 #include "wag.h"
spm71 0:ee6e5c60dd2d 23 #include <stdlib.h>
spm71 0:ee6e5c60dd2d 24 #include <stdio.h>
spm71 0:ee6e5c60dd2d 25 #include <string.h>
spm71 0:ee6e5c60dd2d 26
spm71 18:0e281922212c 27 SPI wag_spi(MOSI, MISO, SCLK);
spm71 0:ee6e5c60dd2d 28 Serial pc(USBTX, USBRX);
spm71 25:896dbc85907e 29 DigitalIn jog_cw(UI_JOG_RIGHT_BUTTON);
spm71 25:896dbc85907e 30 DigitalIn jog_ccw(UI_JOG_LEFT_BUTTON);
spm71 23:3da1d39c1ae9 31 DigitalIn my_button(USER_BUTTON);
spm71 25:896dbc85907e 32 DigitalIn start_button(UI_START_BUTTON);
spm71 25:896dbc85907e 33 DigitalIn cal_button(UI_CAL_BUTTON);
spm71 25:896dbc85907e 34 DigitalIn station_select(UI_STATION_SELECT);
spm71 41:9b293b14b845 35 DigitalIn home_sensor(STP_HOME_SENSOR);
spm71 42:6cba679a4ee4 36 DigitalOut laser(LZR_ENABLE);
phn10 31:4fe43fa66ce8 37 AnalogIn mux_out (MUX_OUT);
phn10 26:3006f5abc0a5 38 BusOut mux_select(MUX_S0, MUX_S1, MUX_S2, MUX_S3);
phn10 44:4c2ba5bbba67 39 int station = -1;
phn10 55:1a25dd75e309 40 int cal_status = NOT_CALIBRATED;
spm71 49:80d4ffabec16 41 float sensor_values[TGT_SENSOR_QUAN * 2];
spm71 52:8987e38851e5 42 int stp_sensor_pos[TGT_SENSOR_QUAN];
spm71 39:abf211b17e3c 43
spm71 20:d23bcd97f2c5 44 struct spi_cfg as1107{
spm71 39:abf211b17e3c 45 SPI_AS1107_ID,
spm71 39:abf211b17e3c 46 DSP_AS1107_NCS,
spm71 39:abf211b17e3c 47 SPI_NO_ID,
spm71 39:abf211b17e3c 48 AS1107_SPI_FREQ,
spm71 39:abf211b17e3c 49 AS1107_SPI_NO_BITS,
spm71 20:d23bcd97f2c5 50 };
spm71 20:d23bcd97f2c5 51
spm71 22:09dd6977576b 52
spm71 0:ee6e5c60dd2d 53 int main(void) {
spm71 20:d23bcd97f2c5 54 initial_setup(as1107);
spm71 20:d23bcd97f2c5 55
spm71 25:896dbc85907e 56 //set all digits to zero
spm71 32:0dc2b4a3eee6 57 spi_send(as1107, 0x0100);
spm71 32:0dc2b4a3eee6 58 spi_send(as1107, 0x0200);
spm71 32:0dc2b4a3eee6 59 spi_send(as1107, 0x0300);
spm71 32:0dc2b4a3eee6 60 spi_send(as1107, 0x0400);
spm71 25:896dbc85907e 61
spm71 56:048b30c9f2a1 62
spm71 39:abf211b17e3c 63 pc.printf("Press user button to test.\n");
phn10 40:4e82369f337c 64
spm71 46:85c4b722baa7 65 test_target_leds();
spm71 46:85c4b722baa7 66 pc.printf("LED's tested\n");
phn10 40:4e82369f337c 67
spm71 46:85c4b722baa7 68 test_phototransistors();
spm71 46:85c4b722baa7 69 pc.printf("Phototransistors tested\n");
spm71 56:048b30c9f2a1 70
spm71 41:9b293b14b845 71 step_test();
spm71 53:389fe53b2642 72 pc.printf("Step motor tested\n");
spm71 56:048b30c9f2a1 73
spm71 46:85c4b722baa7 74 pc.printf("laser test\n");
spm71 46:85c4b722baa7 75 while (my_button.read() == 0);
spm71 46:85c4b722baa7 76 pc.printf("test begin\n");
spm71 43:3b7f326aa096 77 lzr_init();
spm71 49:80d4ffabec16 78 wait(0.5);
spm71 46:85c4b722baa7 79 while (my_button.read() == 0) {
spm71 46:85c4b722baa7 80 lzr_on();
spm71 46:85c4b722baa7 81 wait(0.5);
spm71 46:85c4b722baa7 82 lzr_off();
spm71 46:85c4b722baa7 83 wait(0.5);
spm71 46:85c4b722baa7 84 }
spm71 46:85c4b722baa7 85 lzr_init();
spm71 49:80d4ffabec16 86 pc.printf("Laser tested.\n");
spm71 49:80d4ffabec16 87
phn10 44:4c2ba5bbba67 88 // determine if the wag is connected to station A or station B
phn10 44:4c2ba5bbba67 89 station = station_select.read();
spm71 53:389fe53b2642 90 pc.printf("station in main: %d\n", station);
spm71 56:048b30c9f2a1 91
spm71 52:8987e38851e5 92 /** Part 9: calibration test **/
spm71 52:8987e38851e5 93 pc.printf("Part 9: calibration test begin.\n");
phn10 55:1a25dd75e309 94 stp_calibrate(station, sensor_values, &cal_status);
spm71 52:8987e38851e5 95 pc.printf("Part 9: calibration test done.\n");
spm71 52:8987e38851e5 96
spm71 54:d5a95bc8ffb0 97
phn10 55:1a25dd75e309 98 //stp_sensor_pos[0] = 27;
spm71 54:d5a95bc8ffb0 99 //stp_sensor_pos[1] = 50;
spm71 54:d5a95bc8ffb0 100 //stp_sensor_pos[2] = 72;
spm71 54:d5a95bc8ffb0 101 //stp_sensor_pos[3] = 98;
spm71 54:d5a95bc8ffb0 102 //stp_sensor_pos[4] = 122;
spm71 54:d5a95bc8ffb0 103 //stp_sensor_pos[5] = 146;
spm71 54:d5a95bc8ffb0 104 //stp_sensor_pos[6] = 173;
spm71 54:d5a95bc8ffb0 105 //stp_sensor_pos[7] = 197;
phn10 55:1a25dd75e309 106
spm71 54:d5a95bc8ffb0 107 // turn on laser
spm71 54:d5a95bc8ffb0 108 lzr_on();
spm71 54:d5a95bc8ffb0 109
spm71 52:8987e38851e5 110 /** Part 10: repeatibility test **/
spm71 52:8987e38851e5 111 pc.printf("Part 10: press user button to begin the test.\n");
spm71 52:8987e38851e5 112 while (uti_chk_ubutton() == 0);
spm71 52:8987e38851e5 113 while (uti_chk_ubutton() == 0) {
spm71 56:048b30c9f2a1 114 int sensor_no = 8;
spm71 56:048b30c9f2a1 115 if (station == STATION_B) sensor_no = 0;
spm71 52:8987e38851e5 116
spm71 52:8987e38851e5 117 // move to the left most sensor
phn10 55:1a25dd75e309 118 repeatability_test(0, cal_status);
spm71 54:d5a95bc8ffb0 119
spm71 52:8987e38851e5 120 // read sensor value
spm71 52:8987e38851e5 121 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
spm71 54:d5a95bc8ffb0 122
spm71 52:8987e38851e5 123 // print value
spm71 54:d5a95bc8ffb0 124 pc.printf("Sensor %d: %f\n", sensor_no, sensor_values[sensor_no] * 3.3f);
spm71 52:8987e38851e5 125
spm71 52:8987e38851e5 126 // move to the right most sensor
phn10 55:1a25dd75e309 127 repeatability_test(TGT_SENSOR_QUAN - 1, cal_status);
spm71 54:d5a95bc8ffb0 128
spm71 52:8987e38851e5 129 // read sensor value
spm71 52:8987e38851e5 130 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
spm71 54:d5a95bc8ffb0 131
spm71 52:8987e38851e5 132 // print value
phn10 55:1a25dd75e309 133 pc.printf("Sensor %d: %f\n", sensor_no + TGT_SENSOR_QUAN - 1, sensor_values[sensor_no + TGT_SENSOR_QUAN - 1] * 3.3f);
spm71 52:8987e38851e5 134 }
spm71 52:8987e38851e5 135 pc.printf("Part 10: repeatibility test done.\n");
spm71 39:abf211b17e3c 136 }