Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Impedance_Fast_Circuitry by
Diff: main.cpp
- Revision:
- 32:bf1e079a2ee3
- Parent:
- 31:09e7a8b3c7e7
- Child:
- 33:9806eb964362
--- a/main.cpp Fri Dec 19 21:39:42 2014 +0000 +++ b/main.cpp Thu Jan 08 20:16:19 2015 +0000 @@ -38,7 +38,7 @@ // Analog sampling #define MAX_FADC 6000000 #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz -#define TOTAL_SAMPLES 30000 // originally 30000 for 0.3 ms of sampling. +#define TOTAL_SAMPLES 10000 // 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 @@ -122,6 +122,30 @@ void output_data(uint32_t iteration_number); void timed_sampling(); + +int motorVal = 50; +void releaseMallet() +{ + motorA = 0; + motorB = 1; + wait_ms(10); + motorB = 0; + wait_ms(75); + + motorA = 0.7; + wait_ms(8); + motorA = 0; + } + +void resetMallet() +{ + motorA = 0; + motorB = 1; + wait_ms(153); + motorB = 0; + } + + // Important globabl variables necessary for the sampling every interval int rotary_count = 0; uint32_t last_AMT20_AB_read = 0; @@ -143,46 +167,6 @@ pc.printf("Starting %s\r\n",NAME); - /* - wait(1); - - - motorB = 1; // 0.6 gets it around quickly enough - wait_us(10000); - motorB = 0; - - wait_us(60000); - - motorA = 0.75; - wait_us(5000); - motorA = 0; - - wait_ms(300); - - motorA = 1; - wait_us(50000); - motorA = 0; - - wait_us(45000); - - motorB = 0.75; - wait_us(8000); - motorB = 0; - - - //motorB = 0.2; - //wait_us(5000); - - - motorB = 0; - motorA = 0; - // make sure motors are off - wait_us(PERIOD); - - - - while(1) {} - */ for(int i = 0; i < 86; i++) { @@ -337,6 +321,9 @@ pc.printf("Error while reading data from socket\n\r"); socket->close(); break; + + + //************* this is where data is printed to the screen default: pc.printf("Message from %s:%d\n\r", @@ -370,15 +357,26 @@ case 't': { for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i)); - } + } break; case '1': // run motor and sample { + // release mallet + motorA = 0; + motorB = 1; + wait_ms(10); + motorB = 0; + wait_ms(75); - motorA = 1; - wait_us(10000); + motorA = 0.7; + wait_ms(8); motorA = 0; + + // wait for mallet to drop some before beginning to sample + wait_ms(50); + + // begin sampling 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) client[index].write((void *)"Data\n",5); @@ -423,6 +421,14 @@ } __enable_irq(); + // reset mallet + motorA = 0; + motorB = 1; + wait_ms(138); + motorB = 0; + + + NVIC_SetPriority(ENET_1588_Timer_IRQn,0); NVIC_SetPriority(ENET_Transmit_IRQn,0); NVIC_SetPriority(ENET_Receive_IRQn,0); @@ -441,10 +447,115 @@ } break; - case '2': // run just the motor + case '2': // just reset the mallet { + // release mallet + motorA = 0; + motorB = 1; + wait_ms(10); + motorB = 0; + wait_ms(75); + + motorA = 0.7; + wait_ms(8); + motorA = 0; + + // wait for angle to set to zero + led_blue = 0; + while(!angle_encoder.set_zero()) {} + led_blue = 1; + + // wait for mallet to drop to certin point before beginning to sample + led_red = 0; + int angleVal; + do { + angleVal = angle_encoder.absolute_angle(); + } + while(!(angleVal > 150 && angleVal < 400)); + led_red = 1; + + //char buf[29]; + //sprintf(buf,"AngleVal: %i\n",angleVal); + //client[index].write((void *) buf,29); + + + // begin sampling + 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) + client[index].write((void *)"Data\n",5); + + TotalInd = 1; + + uint32_t AMT20_AB; + rotary_count = 0; + __disable_irq(); + SampleInd = 0; + for(int i = 0; i < TOTAL_SAMPLES; i++) + { + SampleInd = !SampleInd; + sample_array1[i] = adc_hal_get_conversion_value(0, 0); + sample_array2[i] = 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 + AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; + + 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; + } + angle_array[i] = rotary_count; + wait_us(8); } + __enable_irq(); + + // reset mallet + motorA = 0; + motorB = 1; + wait_ms(138); + motorB = 0; + + + + NVIC_SetPriority(ENET_1588_Timer_IRQn,0); + NVIC_SetPriority(ENET_Transmit_IRQn,0); + NVIC_SetPriority(ENET_Receive_IRQn,0); + NVIC_SetPriority(ENET_Error_IRQn,0); + TotalInd = 1; + 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); + TotalInd = 0; + + NVIC_SetPriority(ENET_1588_Timer_IRQn,1); + NVIC_SetPriority(ENET_Transmit_IRQn,1); + NVIC_SetPriority(ENET_Receive_IRQn,1); + NVIC_SetPriority(ENET_Error_IRQn,1); + + } + break; + case '+': + motorVal++; + break; + case '-': + motorVal--; + if(motorVal < 0) motorVal = 0; break; case 'a': if(angle_encoder.set_zero(&rotary_count)) {