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.
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of MalletFirmware by
Diff: main.cpp
- Revision:
- 28:4a833d59897b
- Parent:
- 27:8c2b30c855d1
- Child:
- 29:e6309316c35d
diff -r 8c2b30c855d1 -r 4a833d59897b main.cpp
--- a/main.cpp Fri Dec 05 05:56:33 2014 +0000
+++ b/main.cpp Sun Dec 07 00:56:20 2014 +0000
@@ -34,9 +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 SAMPLING_RATE 20 // 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 LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms
@@ -98,7 +99,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);
@@ -106,8 +107,8 @@
//DigitalIn SW3_switch(PTA4);
//DigitalIn SW2_switch(PTC6);
-DigitalOut StatusSensor(PTC4);
-DigitalOut StatusIndicator2(PTA0); // originally PTD0 but needed for CS for spi
+DigitalOut TotalInd(PTC4);
+DigitalOut SampleInd(PTC5); // originally PTD0 but needed for CS for spi
uint32_t current_sample_index = WAITING_TO_BEGIN;
uint16_t sample_array1[TOTAL_SAMPLES];
@@ -126,8 +127,9 @@
using namespace std;
int main() {
-
//for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;}
+ TotalInd = 0;
+ SampleInd = 0;
led_blue = 1;
led_green = 1;
led_red = 1;
@@ -135,6 +137,29 @@
pc.baud(230400);
pc.printf("Starting %s\r\n",NAME);
+ for(int i = 0; i < 86; i++)
+ {
+ if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
+ }
+
+ //NVIC_SetPriority(SWI_IRQn,0);
+
+ //NVIC_SetPriority(Watchdog_IRQn,0);
+ //NVIC_SetPriority(MCM_IRQn,0);
+ //NVIC_SetPriority(PIT0_IRQn,0);
+ //NVIC_SetPriority(PIT1_IRQn,0);
+ //NVIC_SetPriority(PIT2_IRQn,0);
+ NVIC_SetPriority(PIT3_IRQn,0);
+ //NVIC_SetPriority(LPTimer_IRQn,0);
+
+ NVIC_SetPriority(ADC1_IRQn,0);
+ NVIC_SetPriority(ADC0_IRQn,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);
+
+
// The ethernet setup must be within the first few lines of code, otherwise the program hangs
EthernetInterface interface;
#if STATIC == 1
@@ -149,6 +174,7 @@
pc.printf("Gateway is: %s\n\r", interface.getGateway());
pc.printf("Port is: %i\n\r", PORT);
+
// ethernet setup failed for some reason. Flash yellow light then uC resets itself
if(interface.getIPAddress() == 0)
{
@@ -171,10 +197,10 @@
// Start the sampling loop
current_sample_index = WAITING_TO_BEGIN;
- Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
+ //Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
-
- uint32_t trigger_count = 0;
+ //NVIC_SetPriority(TIMER3_IRQn,0);
+ //pc.printf("Ticker IRQ: %i\r\n", Sampler.irq());
// corresponding duty 1 0 0.7 1 0.75
uint32_t duration[8] = {0, 0, 0, 0, 0, 0, 0, 0};
@@ -213,6 +239,13 @@
wait_ms(500);
led_blue = 1;
+ 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);
+
+ //for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i));
+
do {
// Wait for activity
result = select.wait();
@@ -299,24 +332,75 @@
for(int i = 0; i < 99; i++) sample_array1[i] = i;
client[index].write((void *)&sample_array1,2*99);
break;
+ 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
{
- 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);
+
+
+ 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;
}
- led_green = 1;
- led_blue = 0;
+ angle_array[i] = rotary_count;
+ wait_us(8);
+ }
+ __enable_irq();
- // send samples all at once
+ 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);
- led_blue = 1;
+ 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;
@@ -363,85 +447,9 @@
sprintf(buf,"Angle: %i %i\n",angle_encoder.absolute_angle(), rotary_count);
client[index].write((void *) buf,29);
break;
- }
- /*
-
- // 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]);
- }
-
- }
-
- */
-
-
-
+ }
}
}
-
-
-
-//***************** print a message back to the client
-
- //client[index].write((void *)&sample_array,SAMPLES);
-
-
-
-
- /*for(int i = 1; i <= SAMPLES+1;)
- {
- for(int j = 0; j < 20; j++)
- {
- Timer timeStamp;
- timeStamp.stop();
- timeStamp.reset();
- timeStamp.start();
-
- client[index].write((void *)&sample_array,i);
- int timeStampVar = timeStamp.read_us();
- timeStamp.stop();
-
- pc.printf("*******\r\n%i\r\nTime taken to send data: %i\r\n", i,timeStampVar);
-
- char premessage[40];
- sprintf(premessage, "******\r\n%i\r\nTime taken to send data: %i\r\n", i, timeStampVar);
- std::string response1 = premessage;
- client[index].write((void *)response1.data(), response1.size());
- wait_us(5000);
- }
- if(i == 10000) i = SAMPLES;
- else if(i == SAMPLES) i = i*10;
- else i = i*10;
- }
- std::string endMessage("end");
- client[index].write((void *)endMessage.data(), endMessage.size());
- */
break;
}
}
@@ -450,11 +458,10 @@
}
void timed_sampling() {
- if(switcher) led_red = !led_red;
- /*
+ SampleInd = 1;
//__disable_irq(); // Disable Interrupts
//timeStamp.start();
-
+ /*
// The following performs analog-to-digital conversions - first reading the last conversion - then initiating another
uint32_t A0_value = adc_hal_get_conversion_value(0, 0);
uint32_t A2_value = adc_hal_get_conversion_value(1, 0);
@@ -485,7 +492,7 @@
//current_sample_index = BEGIN_SAMPLING; // Used to force extra time.
if (current_sample_index == WAITING_TO_BEGIN) {}
else
- {
+ {
if (current_sample_index == BEGIN_SAMPLING) {
current_sample_index = FIRST_SAMPLE_INDEX;
}
@@ -498,7 +505,7 @@
current_sample_index = WAITING_TO_BEGIN;
}
else { current_sample_index++; }
- }
+ }
//int tempVar = timeStamp.read_us();
//timeStamp.stop();
@@ -506,6 +513,7 @@
//pc.printf("TimeStamp: %i\r\n", tempVar);
//__enable_irq(); // Enable Interrupts
*/
+ SampleInd = 0;
}
void analog_initialization(PinName pin)
