Same as mallet... but distance

Dependencies:   EthernetInterface NetworkAPI mbed-rtos mbed

Fork of MalletFirmware by Impact-Echo

Files at this revision

API Documentation at this revision

Comitter:
timmey9
Date:
Wed Dec 03 09:21:55 2014 +0000
Parent:
24:a5891669afc5
Child:
26:a00bf9837e03
Commit message:
Everything compiles and works, except when TOTAL_SAMPLES is too large. Works at 100, and ethernet boots up at 10000 but can't connect to MATLAB. Check for memory leaks.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Dec 03 07:18:27 2014 +0000
+++ b/main.cpp	Wed Dec 03 09:21:55 2014 +0000
@@ -43,7 +43,7 @@
 // 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       300 // 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
@@ -113,12 +113,14 @@
 
 //DigitalIn SW3_switch(PTA4);
 //DigitalIn SW2_switch(PTC6);
-
+DigitalOut StatusSensor(PTC4);
+DigitalOut StatusIndicator2(PTA0); // originally PTD0 but needed for CS for spi
 
 
 
 uint32_t current_sample_index = WAITING_TO_BEGIN;
-uint32_t sample_array[TOTAL_SAMPLES];
+uint16_t sample_array1[TOTAL_SAMPLES];
+uint16_t sample_array2[TOTAL_SAMPLES];
 uint16_t angle_array[TOTAL_SAMPLES];
 
 
@@ -137,7 +139,7 @@
  
 int main() {
     
-    for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;}
+    //for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;}
     led_blue = 1;
     led_green = 1;
     led_red = 1;
@@ -164,7 +166,7 @@
     // 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;
+    //int32_t pointer = 0;
     
     double duty_cycle = 0.25;
     
@@ -323,20 +325,112 @@
                                 led_blue = !led_blue;
                                 client[index].write((void *)"Blue LED\n",9);
                                 break;
+                                
                             case 'r':
                                 led_red = !led_red;
                                 client[index].write((void *)"Red LED\n",8);
                                 break;
+                                
                             case 'p':
                                 led_green = !led_green;
                                 client[index].write((void *)"Data\n",5);
-                                client[index].write((void *)&sample_array,TOTAL_SAMPLES);
+                                client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES);
                                 break;
-                            case '1':
-                                // run motor and sample
+                                
+                            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;
+                                trigger_count++;
+                                while (current_sample_index != WAITING_TO_BEGIN){
+                                    wait_us(10);
+                                    }*/
+                                
+                                // below is a crappy form of sampling *******************************
+                                rotary_count = 0;
+                                for(int i = 0; i < TOTAL_SAMPLES; 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;        
+                                    }
+                                    
+                                    sample_array1[i] = A0_value;
+                                    sample_array2[i] = A2_value;
+                                    angle_array[i] = rotary_count;
+                                    
+                                    wait_us(10);
+                                    }
+                                client[index].write((void *)"Data\n",5);
+                                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);
+                                }
                                 break;
-                            case '2':
-                                // run just the motor
+                                
+                            case '2': // run just the motor
+                            {
+                                pc.printf("All duration settings 2:\r\n");
+                                for(int i = 0; i < 8; i++)
+                                {
+                                    pc.printf("Duration[%i]: %i\r\n", i, duration[i]);
+                                    }
+                                
+                                // release mallet
+                                motor.forward(duration[0]); // move motor forward
+                                wait_us(duration[1]); // wait
+                                motor.backward(0.7, duration[2]); // stop motor using reverse
+                                
+                                // time for sampling
+                                wait_us(SAMPLING_RATE*TOTAL_SAMPLES);
+                                
+                                // reset mallet
+                                motor.backward(duration[3]);    // move motor backward
+                                motor.backward(0.75, duration[4]);
+                                motor.backward(duty_cycle, duration[5]);
+                                }
                                 break;
                             case 'a':
                                 if(angle_encoder.set_zero(&rotary_count)) {
@@ -400,9 +494,6 @@
                                 
                             }
                         }
-                    else {
-                        
-                        }
                     
                     
                     
@@ -446,124 +537,6 @@
         }
              
     } while (server.getStatus() == network::Socket::Listening);
-    
-    /*
-    while(1){
-        uint32_t current_SW3 = SW3_switch;      
-        if(pc.readable() > 0)
-        {
-            char temp = pc.getc();
-            while(pc.readable() > 0) pc.getc();
-            
-            if(temp == '1') current_SW3 = 0;
-            else current_SW3 = 1;
-            if(temp == '2') SW2_switch = 0;
-            else SW2_switch = 1;
-            
-            // for angle encoder testing
-            if(temp == 'a') pc.printf(angle_encoder.set_zero(&rotary_count)?"Zero set\r\n":"False\r\n");
-            if(temp == 's') pc.printf("NOP: %x\r\n",angle_encoder.nop());
-            if(temp == 'd') pc.printf("Angle: %i %i\r\n",angle_encoder.absolute_angle(), rotary_count);
-            
-            if(temp == 't')
-            {
-                
-                }
-            
-            
-            // for motor testing
-            if(temp == 'k') // motor backward
-            {
-                motor.backward(duty_cycle, duration[5]);
-                }
-            
-            if(temp == 'u' && duty_cycle < 1.00f) pc.printf("%f \r\n", duty_cycle += 0.01f);
-            if(temp == 'i' && duty_cycle > 0.00f) pc.printf("%f \r\n", duty_cycle -= 0.01f);
-            
-            if(temp == '=') // you can hit the '+' key to increment "duration" without holding down "shift"
-            {
-                if(pointer < 7) pointer++;
-                pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]);
-            }
-            if(temp == '-')
-            {
-                if(pointer > 0) pointer--;
-                pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]);
-            }
-            if(temp == ']') // you can hit the '+' key to increment "duration" without holding down "shift"
-            {
-                duration[pointer] += 1000;
-                pc.printf("             %i\r\n", duration[pointer]);
-            }
-            if(temp == '[')
-            {
-                if(duration[pointer] > 0) duration[pointer]-= 1000;
-                pc.printf("             %i\r\n", duration[pointer]);
-            }
-            
-        }
-        else
-        {
-            SW2_switch = 1;
-            current_SW3 = 1;
-        }
-        
-        if (current_SW3 == 0) {
-            
-            if (SW3_past == 1) {
-                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;
-                trigger_count++;
-                while (current_sample_index != WAITING_TO_BEGIN){
-                    wait_us(10);
-                    }    
-               
-               __disable_irq();    // Disable Interrupts
-               
-               // reset mallet
-               // add code here
-               
-            
-                output_data(trigger_count);
-                }
-            }
-        SW3_past = current_SW3;
-        
-        
-        if (SW2_switch == 0) { // To advance motor
-            pc.printf("All duration settings 2:\r\n");
-            for(int i = 0; i < 8; i++)
-            {
-                pc.printf("Duration[%i]: %i\r\n", i, duration[i]);
-                }
-            
-            
-            SW2_switch = 1;
-            
-            // release mallet
-            motor.forward(duration[0]); // move motor forward
-            wait_us(duration[1]); // wait
-            motor.backward(0.7, duration[2]); // stop motor using reverse
-            
-            // time for sampling
-            wait_us(SAMPLING_RATE*TOTAL_SAMPLES);
-            
-            // reset mallet
-            motor.backward(duration[3]);    // move motor backward
-            motor.backward(0.75, duration[4]);
-            motor.backward(duty_cycle, duration[5]);
-            
-            }
-            */
 }
 
 void timed_sampling() {
@@ -605,7 +578,7 @@
             current_sample_index = FIRST_SAMPLE_INDEX;
             }
             
-        sample_array[current_sample_index] = (A0_value << CHANNEL_STORAGE_OFFSET) | A2_value;
+        sample_array1[current_sample_index] = (A0_value << CHANNEL_STORAGE_OFFSET) | A2_value;
         
         angle_array[current_sample_index] = rotary_count;
         
@@ -662,7 +635,7 @@
     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_array[n] >> CHANNEL_STORAGE_OFFSET), (sample_array[n] & 0xFFFF), angle_array[n]);
+        pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]);
         }
     
 }