Same as mallet... but distance
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of MalletFirmware by
Revision 27:8c2b30c855d1, committed 2014-12-05
- Comitter:
- timmey9
- Date:
- Fri Dec 05 05:56:33 2014 +0000
- Parent:
- 26:a00bf9837e03
- Child:
- 28:4a833d59897b
- Commit message:
- Moved ethernet initialization to beginning of program. The program now compiles.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Dec 03 16:40:51 2014 +0000 +++ b/main.cpp Fri Dec 05 05:56:33 2014 +0000 @@ -9,7 +9,6 @@ #include "NetworkAPI/ip/address.hpp" #include "NetworkAPI/tcp/socket.hpp" - // Angle encoder and motor control #include "AngleEncoder.h" #include "MotorControl.h" @@ -22,7 +21,6 @@ #include "fsl_dspi_hal.h" #include "AngleEncoder.h" - /***************************************** * * Configuration @@ -36,14 +34,10 @@ #define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit) #define INVERT_ANGLE 0 // inverts whether the angle encoder counts up or down - - - - // Analog sampling #define MAX_FADC 6000000 #define SAMPLING_RATE 10000 // In microseconds, so 10 us will be a sampling rate of 100 kHz -#define TOTAL_SAMPLES 10 // originally 30000 for 0.3 ms of sampling. +#define TOTAL_SAMPLES 30000 // originally 30000 for 0.3 ms of sampling. #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms #define FIRST_SAMPLE_INDEX 0 @@ -97,7 +91,6 @@ DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); - // motor control and angle encoder MotorControl motor(PTC2, PTA2, PERIOD, ON_OFF_TIME); // forward, backward, period, safetyPeriod AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz @@ -105,7 +98,7 @@ DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder // Analog sampling -//Ticker Sampler; +Ticker Sampler; //Timer timer; //Timer timeStamp; AnalogIn A0_pin(A0); @@ -116,25 +109,20 @@ DigitalOut StatusSensor(PTC4); DigitalOut StatusIndicator2(PTA0); // originally PTD0 but needed for CS for spi - - uint32_t current_sample_index = WAITING_TO_BEGIN; uint16_t sample_array1[TOTAL_SAMPLES]; uint16_t sample_array2[TOTAL_SAMPLES]; uint16_t angle_array[TOTAL_SAMPLES]; - // Declaration of functions void analog_initialization(PinName pin); void output_data(uint32_t iteration_number); void timed_sampling(); - // Important globabl variables necessary for the sampling every interval int rotary_count = 0; uint32_t last_AMT20_AB_read = 0; -//using namespace network; using namespace std; int main() { @@ -147,51 +135,14 @@ pc.baud(230400); pc.printf("Starting %s\r\n",NAME); - analog_initialization(A0); - analog_initialization(A2); - - // setup this timer - //timeStamp.stop(); - //timeStamp.reset(); - - // Start the sampling loop - current_sample_index = WAITING_TO_BEGIN; - //Sampler.attach_us(&timed_sampling, SAMPLING_RATE); - //__disable_irq(); // Disable Interrupts - //timer.reset(); - //timer.start(); - - uint32_t trigger_count = 0; - - // corresponding duty 1 0 0.7 1 0.75 - uint32_t duration[8] = {10000, 60000, 10000, 39000, 7000, 7000, 0, 0}; - //uint32_t duration[8] = {10000, 100000, 10000, 92000, 25000, 7000, 0, 0}; - //int32_t pointer = 0; - - double duty_cycle = 0.25; - - // flash red LED - led_red = 0; - wait_ms(500); - led_red = 1; - - - - - - - - // initialize ethernet connection and server + // The ethernet setup must be within the first few lines of code, otherwise the program hangs EthernetInterface interface; - #if STATIC == 1 interface.init(IP, MASK, GATEWAY); #else interface.init(); #endif - interface.connect(); - //pc.printf(NAME); pc.printf("IP Address is: %s\n\r", interface.getIPAddress()); pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask()); pc.printf("MAC address is: %s\n\r", interface.getMACAddress()); @@ -213,6 +164,23 @@ NVIC_SystemReset(); } + + analog_initialization(A0); + analog_initialization(A2); + + + // Start the sampling loop + current_sample_index = WAITING_TO_BEGIN; + Sampler.attach_us(&timed_sampling, SAMPLING_RATE); + + + uint32_t trigger_count = 0; + + // corresponding duty 1 0 0.7 1 0.75 + uint32_t duration[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + + double duty_cycle = 0.25; + network::Select select; network::tcp::Socket server; network::tcp::Socket client[MAX_CLIENTS]; @@ -221,14 +189,14 @@ int result = 0; int index = 0; - network::Buffer buffer(TOTAL_SAMPLES); + network::Buffer buffer(50); std::string message(NAME); // Configure the server socket (assume every thing works) server.open(); server.bind(PORT); server.listen(MAX_CLIENTS); - + // Add sockets to the select api select.set(&server, network::Select::Read); for (index = 0; index < MAX_CLIENTS; index++) { @@ -282,12 +250,6 @@ // Send a nice message to the client (tell MATLAB your name client[index].write((void *)message.data(), message.size()); - // read some registers for some info. - //uint32_t* rcr = (uint32_t*) 0x400C0084; - //uint32_t* ecr = (uint32_t*) 0x400C0024; - //pc.printf("RCR register: %x\r\n", *rcr); - //pc.printf("ECR register: %x\r\n", *ecr); - continue; } @@ -334,93 +296,27 @@ case 'p': led_green = !led_green; client[index].write((void *)"Data\n",5); - client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES); + for(int i = 0; i < 99; i++) sample_array1[i] = i; + client[index].write((void *)&sample_array1,2*99); break; case '1': // run motor and sample { - pc.printf("All duration settings:\r\n"); - for(int i = 0; i < 8; i++) - { - pc.printf("Duration[%i]: %i\r\n", i, duration[i]); - } - // release mallet - // add code here - - //__enable_irq(); // Enable Interrupts - - /*current_sample_index = BEGIN_SAMPLING; + led_green = 0; + client[index].write((void *)"Data\n",5); + current_sample_index = BEGIN_SAMPLING; trigger_count++; while (current_sample_index != WAITING_TO_BEGIN){ wait_us(10); - }*/ - - // below is a crappy form of sampling ******************************* - rotary_count = 0; - client[index].write((void *)"Data\n",5); + } + led_green = 1; + led_blue = 0; - for(int i = 0; i < 10000; i++) - { - StatusIndicator2 = !StatusIndicator2; - - uint32_t A0_value = adc_hal_get_conversion_value(0, 0); - uint32_t A2_value = adc_hal_get_conversion_value(1, 0); - BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) - BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) - - // The following updates the rotary counter for the AMT20 sensor - // Put A on PTC0 - // Put B on PTC1 - uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; - //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time. - if (AMT20_AB != last_AMT20_AB_read) - { - // change "INVERT_ANGLE" to change whether relative angle counts up or down. - if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) - #if INVERT_ANGLE == 1 - {rotary_count--;} - else - {rotary_count++;} - #else - {rotary_count++;} - else - {rotary_count--;} - #endif - - last_AMT20_AB_read = AMT20_AB; - } - - - char buf[2]; - buf[1] = (A0_value >> 8) & 0xff; - buf[0] = A0_value & 0xff; - client[index].write((void *)buf,2); - buf[1] = (A2_value >> 8) & 0xff; - buf[0] = A2_value & 0xff; - client[index].write((void *)buf,2); - buf[1] = (rotary_count >> 8) & 0xff; - buf[0] = rotary_count & 0xff; - client[index].write((void *)buf,2); - - //sample_array1[i] = A0_value; - //sample_array2[i] = A2_value; - //angle_array[i] = rotary_count; - - wait_us(10); - } - - //client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES); - //client[index].write((void *)&sample_array2,2*TOTAL_SAMPLES); - //client[index].write((void *)&angle_array,2*TOTAL_SAMPLES); - - //above is a crappy form of sampling ******************************** - - //__disable_irq(); // Disable Interrupts - - // reset mallet - // add code here - - output_data(trigger_count); + // send samples all at once + client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES); + client[index].write((void *)&sample_array2,2*TOTAL_SAMPLES); + client[index].write((void *)&angle_array,2*TOTAL_SAMPLES); + led_blue = 1; } break; @@ -554,7 +450,9 @@ } void timed_sampling() { - __disable_irq(); // Disable Interrupts + if(switcher) led_red = !led_red; + /* + //__disable_irq(); // Disable Interrupts //timeStamp.start(); // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another @@ -592,13 +490,12 @@ current_sample_index = FIRST_SAMPLE_INDEX; } - sample_array1[current_sample_index] = (A0_value << CHANNEL_STORAGE_OFFSET) | A2_value; - + sample_array1[current_sample_index] = A0_value; + sample_array2[current_sample_index] = A2_value; angle_array[current_sample_index] = rotary_count; if (current_sample_index == LAST_SAMPLE_INDEX) { current_sample_index = WAITING_TO_BEGIN; - //led_green = 1; } else { current_sample_index++; } } @@ -607,7 +504,8 @@ //timeStamp.stop(); //timeStamp.reset(); //pc.printf("TimeStamp: %i\r\n", tempVar); - __enable_irq(); // Enable Interrupts + //__enable_irq(); // Enable Interrupts + */ } void analog_initialization(PinName pin) @@ -648,8 +546,15 @@ pc.printf("Iteration: %i\n\r", iteration_number); pc.printf("Sampling rate: %i\n\r", SAMPLING_RATE); pc.printf("Data length: %i\n\r", TOTAL_SAMPLES); - for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) { - pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]); - } + + //for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) { + // pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]); + // } } + +// read some registers for some info. +//uint32_t* rcr = (uint32_t*) 0x400C0084; +//uint32_t* ecr = (uint32_t*) 0x400C0024; +//pc.printf("RCR register: %x\r\n", *rcr); +//pc.printf("ECR register: %x\r\n", *ecr); \ No newline at end of file