
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp@24:a5891669afc5, 2014-12-03 (annotated)
- Committer:
- timmey9
- Date:
- Wed Dec 03 07:18:27 2014 +0000
- Revision:
- 24:a5891669afc5
- Parent:
- 23:9e5141647775
- Child:
- 25:abbc19af13f9
Added angle encoder over ethernet functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 20:f533b3c9296f | 1 | // Server code |
donatien | 0:bb128f0e952f | 2 | #include "mbed.h" |
timmey9 | 24:a5891669afc5 | 3 | #include <stdio.h> |
timmey9 | 22:523e316cbe70 | 4 | |
timmey9 | 22:523e316cbe70 | 5 | // Ethernet |
donatien | 0:bb128f0e952f | 6 | #include "EthernetInterface.h" |
timmey9 | 17:2f978f823020 | 7 | #include "NetworkAPI/buffer.hpp" |
timmey9 | 17:2f978f823020 | 8 | #include "NetworkAPI/select.hpp" |
timmey9 | 17:2f978f823020 | 9 | #include "NetworkAPI/ip/address.hpp" |
timmey9 | 17:2f978f823020 | 10 | #include "NetworkAPI/tcp/socket.hpp" |
timmey9 | 18:b17ddeeb1c09 | 11 | |
timmey9 | 22:523e316cbe70 | 12 | |
timmey9 | 22:523e316cbe70 | 13 | // Angle encoder and motor control |
timmey9 | 22:523e316cbe70 | 14 | #include "AngleEncoder.h" |
timmey9 | 22:523e316cbe70 | 15 | #include "MotorControl.h" |
timmey9 | 22:523e316cbe70 | 16 | |
timmey9 | 22:523e316cbe70 | 17 | // Analog sampling |
timmey9 | 22:523e316cbe70 | 18 | #include "PeripheralNames.h" |
timmey9 | 22:523e316cbe70 | 19 | #include "PeripheralPins.h" |
timmey9 | 22:523e316cbe70 | 20 | #include "fsl_adc_hal.h" |
timmey9 | 22:523e316cbe70 | 21 | #include "fsl_clock_manager.h" |
timmey9 | 22:523e316cbe70 | 22 | #include "fsl_dspi_hal.h" |
timmey9 | 22:523e316cbe70 | 23 | #include "AngleEncoder.h" |
timmey9 | 22:523e316cbe70 | 24 | |
timmey9 | 22:523e316cbe70 | 25 | |
timmey9 | 22:523e316cbe70 | 26 | /***************************************** |
timmey9 | 22:523e316cbe70 | 27 | * |
timmey9 | 22:523e316cbe70 | 28 | * Configuration |
timmey9 | 22:523e316cbe70 | 29 | * |
timmey9 | 22:523e316cbe70 | 30 | * Take the time to set these constants |
timmey9 | 22:523e316cbe70 | 31 | * |
timmey9 | 22:523e316cbe70 | 32 | *****************************************/ |
timmey9 | 21:1fb5023b72af | 33 | #define MALLET 6 // set mallet to a value between 1-7 |
timmey9 | 21:1fb5023b72af | 34 | #define STATIC 1 // set STATIC to 1 for static ip, set STATIC to 0 for dynamic |
timmey9 | 21:1fb5023b72af | 35 | #define PORT 22 // set to a random port number. All the mallets can use the same port number. |
timmey9 | 21:1fb5023b72af | 36 | #define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit) |
timmey9 | 22:523e316cbe70 | 37 | #define INVERT_ANGLE 0 // inverts whether the angle encoder counts up or down |
timmey9 | 22:523e316cbe70 | 38 | |
timmey9 | 22:523e316cbe70 | 39 | |
timmey9 | 21:1fb5023b72af | 40 | |
timmey9 | 21:1fb5023b72af | 41 | |
timmey9 | 21:1fb5023b72af | 42 | |
timmey9 | 22:523e316cbe70 | 43 | // Analog sampling |
timmey9 | 22:523e316cbe70 | 44 | #define MAX_FADC 6000000 |
timmey9 | 22:523e316cbe70 | 45 | #define SAMPLING_RATE 10000 // In microseconds, so 10 us will be a sampling rate of 100 kHz |
timmey9 | 22:523e316cbe70 | 46 | #define TOTAL_SAMPLES 300 // originally 30000 for 0.3 ms of sampling. |
timmey9 | 22:523e316cbe70 | 47 | |
timmey9 | 22:523e316cbe70 | 48 | #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms |
timmey9 | 22:523e316cbe70 | 49 | #define FIRST_SAMPLE_INDEX 0 |
timmey9 | 22:523e316cbe70 | 50 | #define BEGIN_SAMPLING 0xFFFFFFFF |
timmey9 | 22:523e316cbe70 | 51 | #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1) |
timmey9 | 22:523e316cbe70 | 52 | |
timmey9 | 22:523e316cbe70 | 53 | #define CHANNEL_STORAGE_OFFSET 16 // For storing the 16 bits and the 16 bits in a single 32 bit array |
timmey9 | 22:523e316cbe70 | 54 | #define PERIOD 3000 // make sure PERIOD >= ON_OFF_TIME |
timmey9 | 22:523e316cbe70 | 55 | #define ON_OFF_TIME 300 // time it takes for relay to turn on |
timmey9 | 22:523e316cbe70 | 56 | |
timmey9 | 22:523e316cbe70 | 57 | |
timmey9 | 22:523e316cbe70 | 58 | // Ethernet |
timmey9 | 22:523e316cbe70 | 59 | #define GATEWAY "169.254.225.1" |
timmey9 | 22:523e316cbe70 | 60 | #define MASK "255.255.0.0" |
timmey9 | 22:523e316cbe70 | 61 | |
timmey9 | 22:523e316cbe70 | 62 | // used for assign different mallets their ip addresses |
timmey9 | 21:1fb5023b72af | 63 | #if MALLET == 1 |
timmey9 | 22:523e316cbe70 | 64 | #define IP "169.254.225.206" |
timmey9 | 21:1fb5023b72af | 65 | #define NAME "Mallet1\n\r" |
timmey9 | 21:1fb5023b72af | 66 | |
timmey9 | 21:1fb5023b72af | 67 | #elif MALLET == 2 |
timmey9 | 22:523e316cbe70 | 68 | #define IP "169.254.225.207" |
timmey9 | 21:1fb5023b72af | 69 | #define NAME "Mallet2\n\r" |
timmey9 | 21:1fb5023b72af | 70 | |
timmey9 | 21:1fb5023b72af | 71 | #elif MALLET == 3 |
timmey9 | 22:523e316cbe70 | 72 | #define IP "169.254.225.208" |
timmey9 | 21:1fb5023b72af | 73 | #define NAME "Mallet3\n\r" |
timmey9 | 21:1fb5023b72af | 74 | |
timmey9 | 21:1fb5023b72af | 75 | #elif MALLET == 4 |
timmey9 | 22:523e316cbe70 | 76 | #define IP "169.254.225.209" |
timmey9 | 21:1fb5023b72af | 77 | #define NAME "Mallet4\n\r" |
timmey9 | 21:1fb5023b72af | 78 | |
timmey9 | 21:1fb5023b72af | 79 | #elif MALLET == 5 |
timmey9 | 22:523e316cbe70 | 80 | #define IP "169.254.225.210" |
timmey9 | 21:1fb5023b72af | 81 | #define NAME "Mallet5\n\r" |
timmey9 | 21:1fb5023b72af | 82 | |
timmey9 | 21:1fb5023b72af | 83 | #elif MALLET == 6 |
timmey9 | 22:523e316cbe70 | 84 | #define IP "169.254.225.211" |
timmey9 | 21:1fb5023b72af | 85 | #define NAME "Mallet6\n\r" |
timmey9 | 21:1fb5023b72af | 86 | |
timmey9 | 21:1fb5023b72af | 87 | #elif MALLET == 7 |
timmey9 | 22:523e316cbe70 | 88 | #define IP "169.254.225.212" |
timmey9 | 21:1fb5023b72af | 89 | #define NAME "Mallet7\n\r" |
timmey9 | 21:1fb5023b72af | 90 | |
timmey9 | 20:f533b3c9296f | 91 | #endif |
timmey9 | 20:f533b3c9296f | 92 | |
timmey9 | 21:1fb5023b72af | 93 | |
timmey9 | 22:523e316cbe70 | 94 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 95 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 96 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 97 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 98 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 18:b17ddeeb1c09 | 99 | |
timmey9 | 23:9e5141647775 | 100 | |
timmey9 | 22:523e316cbe70 | 101 | // motor control and angle encoder |
timmey9 | 22:523e316cbe70 | 102 | MotorControl motor(PTC2, PTA2, PERIOD, ON_OFF_TIME); // forward, backward, period, safetyPeriod |
timmey9 | 22:523e316cbe70 | 103 | AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz |
timmey9 | 22:523e316cbe70 | 104 | DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder |
timmey9 | 22:523e316cbe70 | 105 | DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder |
timmey9 | 22:523e316cbe70 | 106 | |
timmey9 | 22:523e316cbe70 | 107 | // Analog sampling |
timmey9 | 23:9e5141647775 | 108 | //Ticker Sampler; |
timmey9 | 23:9e5141647775 | 109 | //Timer timer; |
timmey9 | 23:9e5141647775 | 110 | //Timer timeStamp; |
timmey9 | 22:523e316cbe70 | 111 | AnalogIn A0_pin(A0); |
timmey9 | 22:523e316cbe70 | 112 | AnalogIn A2_pin(A2); |
timmey9 | 22:523e316cbe70 | 113 | |
timmey9 | 22:523e316cbe70 | 114 | //DigitalIn SW3_switch(PTA4); |
timmey9 | 22:523e316cbe70 | 115 | //DigitalIn SW2_switch(PTC6); |
timmey9 | 22:523e316cbe70 | 116 | |
timmey9 | 22:523e316cbe70 | 117 | |
timmey9 | 20:f533b3c9296f | 118 | |
timmey9 | 22:523e316cbe70 | 119 | |
timmey9 | 22:523e316cbe70 | 120 | uint32_t current_sample_index = WAITING_TO_BEGIN; |
timmey9 | 22:523e316cbe70 | 121 | uint32_t sample_array[TOTAL_SAMPLES]; |
timmey9 | 22:523e316cbe70 | 122 | uint16_t angle_array[TOTAL_SAMPLES]; |
timmey9 | 22:523e316cbe70 | 123 | |
timmey9 | 22:523e316cbe70 | 124 | |
timmey9 | 22:523e316cbe70 | 125 | // Declaration of functions |
timmey9 | 22:523e316cbe70 | 126 | void analog_initialization(PinName pin); |
timmey9 | 22:523e316cbe70 | 127 | void output_data(uint32_t iteration_number); |
timmey9 | 22:523e316cbe70 | 128 | void timed_sampling(); |
timmey9 | 22:523e316cbe70 | 129 | |
timmey9 | 22:523e316cbe70 | 130 | |
timmey9 | 22:523e316cbe70 | 131 | // Important globabl variables necessary for the sampling every interval |
timmey9 | 22:523e316cbe70 | 132 | int rotary_count = 0; |
timmey9 | 22:523e316cbe70 | 133 | uint32_t last_AMT20_AB_read = 0; |
timmey9 | 23:9e5141647775 | 134 | |
timmey9 | 22:523e316cbe70 | 135 | //using namespace network; |
timmey9 | 22:523e316cbe70 | 136 | using namespace std; |
timmey9 | 17:2f978f823020 | 137 | |
emilmont | 7:65188f4a8c25 | 138 | int main() { |
timmey9 | 23:9e5141647775 | 139 | |
timmey9 | 22:523e316cbe70 | 140 | for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;} |
timmey9 | 22:523e316cbe70 | 141 | led_blue = 1; |
timmey9 | 22:523e316cbe70 | 142 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 143 | led_red = 1; |
timmey9 | 22:523e316cbe70 | 144 | |
timmey9 | 18:b17ddeeb1c09 | 145 | pc.baud(230400); |
timmey9 | 24:a5891669afc5 | 146 | pc.printf("Starting %s\r\n",NAME); |
timmey9 | 23:9e5141647775 | 147 | |
timmey9 | 22:523e316cbe70 | 148 | analog_initialization(A0); |
timmey9 | 22:523e316cbe70 | 149 | analog_initialization(A2); |
timmey9 | 20:f533b3c9296f | 150 | |
timmey9 | 22:523e316cbe70 | 151 | // setup this timer |
timmey9 | 23:9e5141647775 | 152 | //timeStamp.stop(); |
timmey9 | 23:9e5141647775 | 153 | //timeStamp.reset(); |
timmey9 | 22:523e316cbe70 | 154 | |
timmey9 | 22:523e316cbe70 | 155 | // Start the sampling loop |
timmey9 | 22:523e316cbe70 | 156 | current_sample_index = WAITING_TO_BEGIN; |
timmey9 | 23:9e5141647775 | 157 | //Sampler.attach_us(&timed_sampling, SAMPLING_RATE); |
timmey9 | 23:9e5141647775 | 158 | //__disable_irq(); // Disable Interrupts |
timmey9 | 23:9e5141647775 | 159 | //timer.reset(); |
timmey9 | 23:9e5141647775 | 160 | //timer.start(); |
timmey9 | 22:523e316cbe70 | 161 | |
timmey9 | 22:523e316cbe70 | 162 | uint32_t trigger_count = 0; |
timmey9 | 22:523e316cbe70 | 163 | |
timmey9 | 22:523e316cbe70 | 164 | // corresponding duty 1 0 0.7 1 0.75 |
timmey9 | 22:523e316cbe70 | 165 | uint32_t duration[8] = {10000, 60000, 10000, 39000, 7000, 7000, 0, 0}; |
timmey9 | 22:523e316cbe70 | 166 | //uint32_t duration[8] = {10000, 100000, 10000, 92000, 25000, 7000, 0, 0}; |
timmey9 | 22:523e316cbe70 | 167 | int32_t pointer = 0; |
timmey9 | 22:523e316cbe70 | 168 | |
timmey9 | 22:523e316cbe70 | 169 | double duty_cycle = 0.25; |
timmey9 | 23:9e5141647775 | 170 | |
timmey9 | 22:523e316cbe70 | 171 | // flash red LED |
timmey9 | 22:523e316cbe70 | 172 | led_red = 0; |
timmey9 | 22:523e316cbe70 | 173 | wait_ms(500); |
timmey9 | 22:523e316cbe70 | 174 | led_red = 1; |
timmey9 | 22:523e316cbe70 | 175 | |
timmey9 | 22:523e316cbe70 | 176 | |
timmey9 | 22:523e316cbe70 | 177 | |
timmey9 | 22:523e316cbe70 | 178 | |
timmey9 | 22:523e316cbe70 | 179 | |
timmey9 | 22:523e316cbe70 | 180 | |
timmey9 | 22:523e316cbe70 | 181 | |
timmey9 | 22:523e316cbe70 | 182 | // initialize ethernet connection and server |
timmey9 | 17:2f978f823020 | 183 | EthernetInterface interface; |
timmey9 | 22:523e316cbe70 | 184 | |
timmey9 | 21:1fb5023b72af | 185 | #if STATIC == 1 |
timmey9 | 20:f533b3c9296f | 186 | interface.init(IP, MASK, GATEWAY); |
timmey9 | 20:f533b3c9296f | 187 | #else |
timmey9 | 17:2f978f823020 | 188 | interface.init(); |
timmey9 | 20:f533b3c9296f | 189 | #endif |
timmey9 | 21:1fb5023b72af | 190 | |
timmey9 | 17:2f978f823020 | 191 | interface.connect(); |
timmey9 | 24:a5891669afc5 | 192 | //pc.printf(NAME); |
timmey9 | 20:f533b3c9296f | 193 | pc.printf("IP Address is: %s\n\r", interface.getIPAddress()); |
timmey9 | 20:f533b3c9296f | 194 | pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask()); |
timmey9 | 20:f533b3c9296f | 195 | pc.printf("MAC address is: %s\n\r", interface.getMACAddress()); |
timmey9 | 20:f533b3c9296f | 196 | pc.printf("Gateway is: %s\n\r", interface.getGateway()); |
timmey9 | 21:1fb5023b72af | 197 | pc.printf("Port is: %i\n\r", PORT); |
timmey9 | 20:f533b3c9296f | 198 | |
timmey9 | 22:523e316cbe70 | 199 | // ethernet setup failed for some reason. Flash yellow light then uC resets itself |
timmey9 | 23:9e5141647775 | 200 | if(interface.getIPAddress() == 0) |
timmey9 | 22:523e316cbe70 | 201 | { |
timmey9 | 22:523e316cbe70 | 202 | for(int i = 0; i < 5; i++) |
timmey9 | 22:523e316cbe70 | 203 | { |
timmey9 | 22:523e316cbe70 | 204 | led_red = 0; |
timmey9 | 22:523e316cbe70 | 205 | led_green = 0; |
timmey9 | 22:523e316cbe70 | 206 | wait_ms(500); |
timmey9 | 22:523e316cbe70 | 207 | led_red = 1; |
timmey9 | 22:523e316cbe70 | 208 | led_green = 1; |
timmey9 | 22:523e316cbe70 | 209 | wait_ms(1000); |
timmey9 | 22:523e316cbe70 | 210 | } |
timmey9 | 22:523e316cbe70 | 211 | NVIC_SystemReset(); |
timmey9 | 23:9e5141647775 | 212 | } |
timmey9 | 22:523e316cbe70 | 213 | |
timmey9 | 22:523e316cbe70 | 214 | network::Select select; |
timmey9 | 22:523e316cbe70 | 215 | network::tcp::Socket server; |
timmey9 | 22:523e316cbe70 | 216 | network::tcp::Socket client[MAX_CLIENTS]; |
timmey9 | 22:523e316cbe70 | 217 | network::tcp::Socket *socket = NULL; |
timmey9 | 17:2f978f823020 | 218 | |
timmey9 | 17:2f978f823020 | 219 | int result = 0; |
timmey9 | 17:2f978f823020 | 220 | int index = 0; |
timmey9 | 17:2f978f823020 | 221 | |
timmey9 | 22:523e316cbe70 | 222 | network::Buffer buffer(TOTAL_SAMPLES); |
timmey9 | 21:1fb5023b72af | 223 | std::string message(NAME); |
timmey9 | 17:2f978f823020 | 224 | |
timmey9 | 21:1fb5023b72af | 225 | // Configure the server socket (assume every thing works) |
timmey9 | 17:2f978f823020 | 226 | server.open(); |
timmey9 | 20:f533b3c9296f | 227 | server.bind(PORT); |
timmey9 | 17:2f978f823020 | 228 | server.listen(MAX_CLIENTS); |
timmey9 | 17:2f978f823020 | 229 | |
timmey9 | 17:2f978f823020 | 230 | // Add sockets to the select api |
timmey9 | 22:523e316cbe70 | 231 | select.set(&server, network::Select::Read); |
timmey9 | 17:2f978f823020 | 232 | for (index = 0; index < MAX_CLIENTS; index++) { |
timmey9 | 22:523e316cbe70 | 233 | select.set(&client[index], network::Select::Read); |
timmey9 | 17:2f978f823020 | 234 | } |
timmey9 | 22:523e316cbe70 | 235 | |
timmey9 | 22:523e316cbe70 | 236 | led_red = 1; |
timmey9 | 22:523e316cbe70 | 237 | led_green = 1; |
timmey9 | 22:523e316cbe70 | 238 | led_blue = 0; |
timmey9 | 23:9e5141647775 | 239 | wait_ms(500); |
timmey9 | 22:523e316cbe70 | 240 | led_blue = 1; |
timmey9 | 23:9e5141647775 | 241 | wait_ms(200); |
timmey9 | 23:9e5141647775 | 242 | led_blue = 0; |
timmey9 | 23:9e5141647775 | 243 | wait_ms(500); |
timmey9 | 23:9e5141647775 | 244 | led_blue = 1; |
timmey9 | 22:523e316cbe70 | 245 | |
timmey9 | 17:2f978f823020 | 246 | do { |
timmey9 | 17:2f978f823020 | 247 | // Wait for activity |
timmey9 | 17:2f978f823020 | 248 | result = select.wait(); |
timmey9 | 17:2f978f823020 | 249 | if (result < -1) { |
timmey9 | 18:b17ddeeb1c09 | 250 | pc.printf("Failed to select\n\r"); |
emilmont | 7:65188f4a8c25 | 251 | break; |
timmey9 | 16:c3f922f61b8f | 252 | } |
timmey9 | 17:2f978f823020 | 253 | |
timmey9 | 17:2f978f823020 | 254 | // Get the first socket |
timmey9 | 22:523e316cbe70 | 255 | socket = (network::tcp::Socket *)select.getReadable(); |
timmey9 | 17:2f978f823020 | 256 | |
timmey9 | 22:523e316cbe70 | 257 | for (; socket != NULL; socket = (network::tcp::Socket *)select.getReadable()) { |
timmey9 | 17:2f978f823020 | 258 | // Check if there was a connection request. |
timmey9 | 17:2f978f823020 | 259 | if (socket->getHandle() == server.getHandle()) { |
timmey9 | 17:2f978f823020 | 260 | // Find an unused client |
timmey9 | 17:2f978f823020 | 261 | for (index = 0; index < MAX_CLIENTS; index++) { |
timmey9 | 17:2f978f823020 | 262 | if (client[index].getStatus() == network::Socket::Closed) { |
timmey9 | 17:2f978f823020 | 263 | break; |
timmey9 | 17:2f978f823020 | 264 | } |
timmey9 | 17:2f978f823020 | 265 | } |
timmey9 | 17:2f978f823020 | 266 | |
timmey9 | 17:2f978f823020 | 267 | // Maximum connections reached |
timmey9 | 17:2f978f823020 | 268 | if (index == MAX_CLIENTS) { |
timmey9 | 18:b17ddeeb1c09 | 269 | pc.printf("Maximum connections reached\n\r"); |
timmey9 | 21:1fb5023b72af | 270 | wait(1); |
timmey9 | 17:2f978f823020 | 271 | continue; |
timmey9 | 17:2f978f823020 | 272 | } |
timmey9 | 20:f533b3c9296f | 273 | |
timmey9 | 17:2f978f823020 | 274 | // Accept the client |
timmey9 | 17:2f978f823020 | 275 | socket->accept(client[index]); |
timmey9 | 18:b17ddeeb1c09 | 276 | pc.printf("Client connected %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 277 | client[index].getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 278 | client[index].getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 279 | |
timmey9 | 21:1fb5023b72af | 280 | // Send a nice message to the client (tell MATLAB your name |
timmey9 | 17:2f978f823020 | 281 | client[index].write((void *)message.data(), message.size()); |
timmey9 | 20:f533b3c9296f | 282 | |
timmey9 | 20:f533b3c9296f | 283 | // read some registers for some info. |
timmey9 | 20:f533b3c9296f | 284 | //uint32_t* rcr = (uint32_t*) 0x400C0084; |
timmey9 | 20:f533b3c9296f | 285 | //uint32_t* ecr = (uint32_t*) 0x400C0024; |
timmey9 | 20:f533b3c9296f | 286 | //pc.printf("RCR register: %x\r\n", *rcr); |
timmey9 | 20:f533b3c9296f | 287 | //pc.printf("ECR register: %x\r\n", *ecr); |
timmey9 | 20:f533b3c9296f | 288 | |
timmey9 | 17:2f978f823020 | 289 | continue; |
timmey9 | 16:c3f922f61b8f | 290 | } |
timmey9 | 20:f533b3c9296f | 291 | |
timmey9 | 17:2f978f823020 | 292 | // It was not the server socket, so it must be a client talking to us. |
timmey9 | 17:2f978f823020 | 293 | switch (socket->read(buffer)) { |
timmey9 | 17:2f978f823020 | 294 | case 0: |
timmey9 | 17:2f978f823020 | 295 | // Remote end disconnected |
timmey9 | 18:b17ddeeb1c09 | 296 | pc.printf("Client disconnected %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 297 | socket->getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 298 | socket->getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 299 | |
timmey9 | 17:2f978f823020 | 300 | // Close socket |
timmey9 | 17:2f978f823020 | 301 | socket->close(); |
timmey9 | 17:2f978f823020 | 302 | break; |
timmey9 | 20:f533b3c9296f | 303 | |
timmey9 | 17:2f978f823020 | 304 | case -1: |
timmey9 | 18:b17ddeeb1c09 | 305 | pc.printf("Error while reading data from socket\n\r"); |
timmey9 | 17:2f978f823020 | 306 | socket->close(); |
timmey9 | 17:2f978f823020 | 307 | break; |
timmey9 | 20:f533b3c9296f | 308 | //************* this is where data is printed to the screen |
timmey9 | 17:2f978f823020 | 309 | default: |
timmey9 | 18:b17ddeeb1c09 | 310 | pc.printf("Message from %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 311 | socket->getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 312 | socket->getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 313 | |
timmey9 | 21:1fb5023b72af | 314 | pc.printf("%s\n\r", (char *)buffer.data()); |
timmey9 | 21:1fb5023b72af | 315 | |
timmey9 | 21:1fb5023b72af | 316 | // read first character for command |
timmey9 | 21:1fb5023b72af | 317 | char command[2]; |
timmey9 | 21:1fb5023b72af | 318 | buffer.read(command,2,0); |
timmey9 | 21:1fb5023b72af | 319 | if(command[1] == ':') { |
timmey9 | 21:1fb5023b72af | 320 | switch(command[0]) |
timmey9 | 21:1fb5023b72af | 321 | { |
timmey9 | 21:1fb5023b72af | 322 | case 'b': |
timmey9 | 21:1fb5023b72af | 323 | led_blue = !led_blue; |
timmey9 | 21:1fb5023b72af | 324 | client[index].write((void *)"Blue LED\n",9); |
timmey9 | 21:1fb5023b72af | 325 | break; |
timmey9 | 21:1fb5023b72af | 326 | case 'r': |
timmey9 | 21:1fb5023b72af | 327 | led_red = !led_red; |
timmey9 | 21:1fb5023b72af | 328 | client[index].write((void *)"Red LED\n",8); |
timmey9 | 21:1fb5023b72af | 329 | break; |
timmey9 | 21:1fb5023b72af | 330 | case 'p': |
timmey9 | 21:1fb5023b72af | 331 | led_green = !led_green; |
timmey9 | 21:1fb5023b72af | 332 | client[index].write((void *)"Data\n",5); |
timmey9 | 22:523e316cbe70 | 333 | client[index].write((void *)&sample_array,TOTAL_SAMPLES); |
timmey9 | 24:a5891669afc5 | 334 | break; |
timmey9 | 24:a5891669afc5 | 335 | case '1': |
timmey9 | 24:a5891669afc5 | 336 | // run motor and sample |
timmey9 | 24:a5891669afc5 | 337 | break; |
timmey9 | 24:a5891669afc5 | 338 | case '2': |
timmey9 | 24:a5891669afc5 | 339 | // run just the motor |
timmey9 | 24:a5891669afc5 | 340 | break; |
timmey9 | 24:a5891669afc5 | 341 | case 'a': |
timmey9 | 24:a5891669afc5 | 342 | if(angle_encoder.set_zero(&rotary_count)) { |
timmey9 | 24:a5891669afc5 | 343 | client[index].write((void *) "Zero set\n",9); |
timmey9 | 24:a5891669afc5 | 344 | } |
timmey9 | 24:a5891669afc5 | 345 | else { |
timmey9 | 24:a5891669afc5 | 346 | client[index].write((void *) "Zero NOT set\n",13); |
timmey9 | 24:a5891669afc5 | 347 | } |
timmey9 | 24:a5891669afc5 | 348 | break; |
timmey9 | 24:a5891669afc5 | 349 | case 's': |
timmey9 | 24:a5891669afc5 | 350 | { |
timmey9 | 24:a5891669afc5 | 351 | char buf[16]; |
timmey9 | 24:a5891669afc5 | 352 | sprintf(buf,"NOP: %x\n",angle_encoder.nop()); |
timmey9 | 24:a5891669afc5 | 353 | client[index].write((void *) buf,16); |
timmey9 | 24:a5891669afc5 | 354 | break; |
timmey9 | 24:a5891669afc5 | 355 | } |
timmey9 | 24:a5891669afc5 | 356 | case 'd': |
timmey9 | 24:a5891669afc5 | 357 | { |
timmey9 | 24:a5891669afc5 | 358 | char buf[29]; |
timmey9 | 24:a5891669afc5 | 359 | sprintf(buf,"Angle: %i %i\n",angle_encoder.absolute_angle(), rotary_count); |
timmey9 | 24:a5891669afc5 | 360 | client[index].write((void *) buf,29); |
timmey9 | 24:a5891669afc5 | 361 | break; |
timmey9 | 24:a5891669afc5 | 362 | } |
timmey9 | 24:a5891669afc5 | 363 | /* |
timmey9 | 24:a5891669afc5 | 364 | |
timmey9 | 24:a5891669afc5 | 365 | // for motor testing |
timmey9 | 24:a5891669afc5 | 366 | if(temp == 'k') // motor backward |
timmey9 | 24:a5891669afc5 | 367 | { |
timmey9 | 24:a5891669afc5 | 368 | motor.backward(duty_cycle, duration[5]); |
timmey9 | 24:a5891669afc5 | 369 | } |
timmey9 | 24:a5891669afc5 | 370 | |
timmey9 | 24:a5891669afc5 | 371 | if(temp == 'u' && duty_cycle < 1.00f) pc.printf("%f \r\n", duty_cycle += 0.01f); |
timmey9 | 24:a5891669afc5 | 372 | if(temp == 'i' && duty_cycle > 0.00f) pc.printf("%f \r\n", duty_cycle -= 0.01f); |
timmey9 | 24:a5891669afc5 | 373 | |
timmey9 | 24:a5891669afc5 | 374 | if(temp == '=') // you can hit the '+' key to increment "duration" without holding down "shift" |
timmey9 | 24:a5891669afc5 | 375 | { |
timmey9 | 24:a5891669afc5 | 376 | if(pointer < 7) pointer++; |
timmey9 | 24:a5891669afc5 | 377 | pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]); |
timmey9 | 24:a5891669afc5 | 378 | } |
timmey9 | 24:a5891669afc5 | 379 | if(temp == '-') |
timmey9 | 24:a5891669afc5 | 380 | { |
timmey9 | 24:a5891669afc5 | 381 | if(pointer > 0) pointer--; |
timmey9 | 24:a5891669afc5 | 382 | pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]); |
timmey9 | 24:a5891669afc5 | 383 | } |
timmey9 | 24:a5891669afc5 | 384 | if(temp == ']') // you can hit the '+' key to increment "duration" without holding down "shift" |
timmey9 | 24:a5891669afc5 | 385 | { |
timmey9 | 24:a5891669afc5 | 386 | duration[pointer] += 1000; |
timmey9 | 24:a5891669afc5 | 387 | pc.printf(" %i\r\n", duration[pointer]); |
timmey9 | 24:a5891669afc5 | 388 | } |
timmey9 | 24:a5891669afc5 | 389 | if(temp == '[') |
timmey9 | 24:a5891669afc5 | 390 | { |
timmey9 | 24:a5891669afc5 | 391 | if(duration[pointer] > 0) duration[pointer]-= 1000; |
timmey9 | 24:a5891669afc5 | 392 | pc.printf(" %i\r\n", duration[pointer]); |
timmey9 | 24:a5891669afc5 | 393 | } |
timmey9 | 24:a5891669afc5 | 394 | |
timmey9 | 24:a5891669afc5 | 395 | } |
timmey9 | 24:a5891669afc5 | 396 | |
timmey9 | 24:a5891669afc5 | 397 | */ |
timmey9 | 24:a5891669afc5 | 398 | |
timmey9 | 24:a5891669afc5 | 399 | |
timmey9 | 24:a5891669afc5 | 400 | |
timmey9 | 21:1fb5023b72af | 401 | } |
timmey9 | 21:1fb5023b72af | 402 | } |
timmey9 | 21:1fb5023b72af | 403 | else { |
timmey9 | 21:1fb5023b72af | 404 | |
timmey9 | 21:1fb5023b72af | 405 | } |
timmey9 | 21:1fb5023b72af | 406 | |
timmey9 | 21:1fb5023b72af | 407 | |
timmey9 | 20:f533b3c9296f | 408 | |
timmey9 | 20:f533b3c9296f | 409 | //***************** print a message back to the client |
timmey9 | 21:1fb5023b72af | 410 | |
timmey9 | 21:1fb5023b72af | 411 | //client[index].write((void *)&sample_array,SAMPLES); |
timmey9 | 21:1fb5023b72af | 412 | |
timmey9 | 21:1fb5023b72af | 413 | |
timmey9 | 21:1fb5023b72af | 414 | |
timmey9 | 20:f533b3c9296f | 415 | |
timmey9 | 21:1fb5023b72af | 416 | /*for(int i = 1; i <= SAMPLES+1;) |
timmey9 | 21:1fb5023b72af | 417 | { |
timmey9 | 21:1fb5023b72af | 418 | for(int j = 0; j < 20; j++) |
timmey9 | 21:1fb5023b72af | 419 | { |
timmey9 | 21:1fb5023b72af | 420 | Timer timeStamp; |
timmey9 | 21:1fb5023b72af | 421 | timeStamp.stop(); |
timmey9 | 21:1fb5023b72af | 422 | timeStamp.reset(); |
timmey9 | 21:1fb5023b72af | 423 | timeStamp.start(); |
timmey9 | 21:1fb5023b72af | 424 | |
timmey9 | 21:1fb5023b72af | 425 | client[index].write((void *)&sample_array,i); |
timmey9 | 21:1fb5023b72af | 426 | int timeStampVar = timeStamp.read_us(); |
timmey9 | 21:1fb5023b72af | 427 | timeStamp.stop(); |
timmey9 | 21:1fb5023b72af | 428 | |
timmey9 | 21:1fb5023b72af | 429 | pc.printf("*******\r\n%i\r\nTime taken to send data: %i\r\n", i,timeStampVar); |
timmey9 | 21:1fb5023b72af | 430 | |
timmey9 | 21:1fb5023b72af | 431 | char premessage[40]; |
timmey9 | 21:1fb5023b72af | 432 | sprintf(premessage, "******\r\n%i\r\nTime taken to send data: %i\r\n", i, timeStampVar); |
timmey9 | 21:1fb5023b72af | 433 | std::string response1 = premessage; |
timmey9 | 21:1fb5023b72af | 434 | client[index].write((void *)response1.data(), response1.size()); |
timmey9 | 21:1fb5023b72af | 435 | wait_us(5000); |
timmey9 | 21:1fb5023b72af | 436 | } |
timmey9 | 21:1fb5023b72af | 437 | if(i == 10000) i = SAMPLES; |
timmey9 | 21:1fb5023b72af | 438 | else if(i == SAMPLES) i = i*10; |
timmey9 | 21:1fb5023b72af | 439 | else i = i*10; |
timmey9 | 21:1fb5023b72af | 440 | } |
timmey9 | 21:1fb5023b72af | 441 | std::string endMessage("end"); |
timmey9 | 21:1fb5023b72af | 442 | client[index].write((void *)endMessage.data(), endMessage.size()); |
timmey9 | 21:1fb5023b72af | 443 | */ |
timmey9 | 17:2f978f823020 | 444 | break; |
timmey9 | 17:2f978f823020 | 445 | } |
timmey9 | 16:c3f922f61b8f | 446 | } |
timmey9 | 17:2f978f823020 | 447 | |
timmey9 | 17:2f978f823020 | 448 | } while (server.getStatus() == network::Socket::Listening); |
timmey9 | 24:a5891669afc5 | 449 | |
timmey9 | 24:a5891669afc5 | 450 | /* |
timmey9 | 24:a5891669afc5 | 451 | while(1){ |
timmey9 | 24:a5891669afc5 | 452 | uint32_t current_SW3 = SW3_switch; |
timmey9 | 24:a5891669afc5 | 453 | if(pc.readable() > 0) |
timmey9 | 24:a5891669afc5 | 454 | { |
timmey9 | 24:a5891669afc5 | 455 | char temp = pc.getc(); |
timmey9 | 24:a5891669afc5 | 456 | while(pc.readable() > 0) pc.getc(); |
timmey9 | 24:a5891669afc5 | 457 | |
timmey9 | 24:a5891669afc5 | 458 | if(temp == '1') current_SW3 = 0; |
timmey9 | 24:a5891669afc5 | 459 | else current_SW3 = 1; |
timmey9 | 24:a5891669afc5 | 460 | if(temp == '2') SW2_switch = 0; |
timmey9 | 24:a5891669afc5 | 461 | else SW2_switch = 1; |
timmey9 | 24:a5891669afc5 | 462 | |
timmey9 | 24:a5891669afc5 | 463 | // for angle encoder testing |
timmey9 | 24:a5891669afc5 | 464 | if(temp == 'a') pc.printf(angle_encoder.set_zero(&rotary_count)?"Zero set\r\n":"False\r\n"); |
timmey9 | 24:a5891669afc5 | 465 | if(temp == 's') pc.printf("NOP: %x\r\n",angle_encoder.nop()); |
timmey9 | 24:a5891669afc5 | 466 | if(temp == 'd') pc.printf("Angle: %i %i\r\n",angle_encoder.absolute_angle(), rotary_count); |
timmey9 | 24:a5891669afc5 | 467 | |
timmey9 | 24:a5891669afc5 | 468 | if(temp == 't') |
timmey9 | 24:a5891669afc5 | 469 | { |
timmey9 | 24:a5891669afc5 | 470 | |
timmey9 | 24:a5891669afc5 | 471 | } |
timmey9 | 24:a5891669afc5 | 472 | |
timmey9 | 24:a5891669afc5 | 473 | |
timmey9 | 24:a5891669afc5 | 474 | // for motor testing |
timmey9 | 24:a5891669afc5 | 475 | if(temp == 'k') // motor backward |
timmey9 | 24:a5891669afc5 | 476 | { |
timmey9 | 24:a5891669afc5 | 477 | motor.backward(duty_cycle, duration[5]); |
timmey9 | 24:a5891669afc5 | 478 | } |
timmey9 | 24:a5891669afc5 | 479 | |
timmey9 | 24:a5891669afc5 | 480 | if(temp == 'u' && duty_cycle < 1.00f) pc.printf("%f \r\n", duty_cycle += 0.01f); |
timmey9 | 24:a5891669afc5 | 481 | if(temp == 'i' && duty_cycle > 0.00f) pc.printf("%f \r\n", duty_cycle -= 0.01f); |
timmey9 | 24:a5891669afc5 | 482 | |
timmey9 | 24:a5891669afc5 | 483 | if(temp == '=') // you can hit the '+' key to increment "duration" without holding down "shift" |
timmey9 | 24:a5891669afc5 | 484 | { |
timmey9 | 24:a5891669afc5 | 485 | if(pointer < 7) pointer++; |
timmey9 | 24:a5891669afc5 | 486 | pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]); |
timmey9 | 24:a5891669afc5 | 487 | } |
timmey9 | 24:a5891669afc5 | 488 | if(temp == '-') |
timmey9 | 24:a5891669afc5 | 489 | { |
timmey9 | 24:a5891669afc5 | 490 | if(pointer > 0) pointer--; |
timmey9 | 24:a5891669afc5 | 491 | pc.printf("Duration[%i]: %i\r\n",pointer, duration[pointer]); |
timmey9 | 24:a5891669afc5 | 492 | } |
timmey9 | 24:a5891669afc5 | 493 | if(temp == ']') // you can hit the '+' key to increment "duration" without holding down "shift" |
timmey9 | 24:a5891669afc5 | 494 | { |
timmey9 | 24:a5891669afc5 | 495 | duration[pointer] += 1000; |
timmey9 | 24:a5891669afc5 | 496 | pc.printf(" %i\r\n", duration[pointer]); |
timmey9 | 24:a5891669afc5 | 497 | } |
timmey9 | 24:a5891669afc5 | 498 | if(temp == '[') |
timmey9 | 24:a5891669afc5 | 499 | { |
timmey9 | 24:a5891669afc5 | 500 | if(duration[pointer] > 0) duration[pointer]-= 1000; |
timmey9 | 24:a5891669afc5 | 501 | pc.printf(" %i\r\n", duration[pointer]); |
timmey9 | 24:a5891669afc5 | 502 | } |
timmey9 | 24:a5891669afc5 | 503 | |
timmey9 | 24:a5891669afc5 | 504 | } |
timmey9 | 24:a5891669afc5 | 505 | else |
timmey9 | 24:a5891669afc5 | 506 | { |
timmey9 | 24:a5891669afc5 | 507 | SW2_switch = 1; |
timmey9 | 24:a5891669afc5 | 508 | current_SW3 = 1; |
timmey9 | 24:a5891669afc5 | 509 | } |
timmey9 | 24:a5891669afc5 | 510 | |
timmey9 | 24:a5891669afc5 | 511 | if (current_SW3 == 0) { |
timmey9 | 24:a5891669afc5 | 512 | |
timmey9 | 24:a5891669afc5 | 513 | if (SW3_past == 1) { |
timmey9 | 24:a5891669afc5 | 514 | pc.printf("All duration settings:\r\n"); |
timmey9 | 24:a5891669afc5 | 515 | for(int i = 0; i < 8; i++) |
timmey9 | 24:a5891669afc5 | 516 | { |
timmey9 | 24:a5891669afc5 | 517 | pc.printf("Duration[%i]: %i\r\n", i, duration[i]); |
timmey9 | 24:a5891669afc5 | 518 | } |
timmey9 | 24:a5891669afc5 | 519 | // release mallet |
timmey9 | 24:a5891669afc5 | 520 | // add code here |
timmey9 | 24:a5891669afc5 | 521 | |
timmey9 | 24:a5891669afc5 | 522 | __enable_irq(); // Enable Interrupts |
timmey9 | 24:a5891669afc5 | 523 | |
timmey9 | 24:a5891669afc5 | 524 | current_sample_index = BEGIN_SAMPLING; |
timmey9 | 24:a5891669afc5 | 525 | trigger_count++; |
timmey9 | 24:a5891669afc5 | 526 | while (current_sample_index != WAITING_TO_BEGIN){ |
timmey9 | 24:a5891669afc5 | 527 | wait_us(10); |
timmey9 | 24:a5891669afc5 | 528 | } |
timmey9 | 24:a5891669afc5 | 529 | |
timmey9 | 24:a5891669afc5 | 530 | __disable_irq(); // Disable Interrupts |
timmey9 | 24:a5891669afc5 | 531 | |
timmey9 | 24:a5891669afc5 | 532 | // reset mallet |
timmey9 | 24:a5891669afc5 | 533 | // add code here |
timmey9 | 24:a5891669afc5 | 534 | |
timmey9 | 24:a5891669afc5 | 535 | |
timmey9 | 24:a5891669afc5 | 536 | output_data(trigger_count); |
timmey9 | 24:a5891669afc5 | 537 | } |
timmey9 | 24:a5891669afc5 | 538 | } |
timmey9 | 24:a5891669afc5 | 539 | SW3_past = current_SW3; |
timmey9 | 24:a5891669afc5 | 540 | |
timmey9 | 24:a5891669afc5 | 541 | |
timmey9 | 24:a5891669afc5 | 542 | if (SW2_switch == 0) { // To advance motor |
timmey9 | 24:a5891669afc5 | 543 | pc.printf("All duration settings 2:\r\n"); |
timmey9 | 24:a5891669afc5 | 544 | for(int i = 0; i < 8; i++) |
timmey9 | 24:a5891669afc5 | 545 | { |
timmey9 | 24:a5891669afc5 | 546 | pc.printf("Duration[%i]: %i\r\n", i, duration[i]); |
timmey9 | 24:a5891669afc5 | 547 | } |
timmey9 | 24:a5891669afc5 | 548 | |
timmey9 | 24:a5891669afc5 | 549 | |
timmey9 | 24:a5891669afc5 | 550 | SW2_switch = 1; |
timmey9 | 24:a5891669afc5 | 551 | |
timmey9 | 24:a5891669afc5 | 552 | // release mallet |
timmey9 | 24:a5891669afc5 | 553 | motor.forward(duration[0]); // move motor forward |
timmey9 | 24:a5891669afc5 | 554 | wait_us(duration[1]); // wait |
timmey9 | 24:a5891669afc5 | 555 | motor.backward(0.7, duration[2]); // stop motor using reverse |
timmey9 | 24:a5891669afc5 | 556 | |
timmey9 | 24:a5891669afc5 | 557 | // time for sampling |
timmey9 | 24:a5891669afc5 | 558 | wait_us(SAMPLING_RATE*TOTAL_SAMPLES); |
timmey9 | 24:a5891669afc5 | 559 | |
timmey9 | 24:a5891669afc5 | 560 | // reset mallet |
timmey9 | 24:a5891669afc5 | 561 | motor.backward(duration[3]); // move motor backward |
timmey9 | 24:a5891669afc5 | 562 | motor.backward(0.75, duration[4]); |
timmey9 | 24:a5891669afc5 | 563 | motor.backward(duty_cycle, duration[5]); |
timmey9 | 24:a5891669afc5 | 564 | |
timmey9 | 24:a5891669afc5 | 565 | } |
timmey9 | 24:a5891669afc5 | 566 | */ |
timmey9 | 22:523e316cbe70 | 567 | } |
timmey9 | 23:9e5141647775 | 568 | |
timmey9 | 22:523e316cbe70 | 569 | void timed_sampling() { |
timmey9 | 22:523e316cbe70 | 570 | __disable_irq(); // Disable Interrupts |
timmey9 | 23:9e5141647775 | 571 | //timeStamp.start(); |
timmey9 | 22:523e316cbe70 | 572 | |
timmey9 | 22:523e316cbe70 | 573 | // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another |
timmey9 | 22:523e316cbe70 | 574 | uint32_t A0_value = adc_hal_get_conversion_value(0, 0); |
timmey9 | 22:523e316cbe70 | 575 | uint32_t A2_value = adc_hal_get_conversion_value(1, 0); |
timmey9 | 22:523e316cbe70 | 576 | BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) |
timmey9 | 22:523e316cbe70 | 577 | BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) |
timmey9 | 22:523e316cbe70 | 578 | |
timmey9 | 22:523e316cbe70 | 579 | // The following updates the rotary counter for the AMT20 sensor |
timmey9 | 22:523e316cbe70 | 580 | // Put A on PTC0 |
timmey9 | 22:523e316cbe70 | 581 | // Put B on PTC1 |
timmey9 | 22:523e316cbe70 | 582 | uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; |
timmey9 | 22:523e316cbe70 | 583 | //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time. |
timmey9 | 22:523e316cbe70 | 584 | if (AMT20_AB != last_AMT20_AB_read) |
timmey9 | 22:523e316cbe70 | 585 | { |
timmey9 | 22:523e316cbe70 | 586 | // change "INVERT_ANGLE" to change whether relative angle counts up or down. |
timmey9 | 22:523e316cbe70 | 587 | if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) |
timmey9 | 22:523e316cbe70 | 588 | #if INVERT_ANGLE == 1 |
timmey9 | 22:523e316cbe70 | 589 | {rotary_count--;} |
timmey9 | 22:523e316cbe70 | 590 | else |
timmey9 | 22:523e316cbe70 | 591 | {rotary_count++;} |
timmey9 | 22:523e316cbe70 | 592 | #else |
timmey9 | 22:523e316cbe70 | 593 | {rotary_count++;} |
timmey9 | 22:523e316cbe70 | 594 | else |
timmey9 | 22:523e316cbe70 | 595 | {rotary_count--;} |
timmey9 | 22:523e316cbe70 | 596 | #endif |
timmey9 | 22:523e316cbe70 | 597 | |
timmey9 | 22:523e316cbe70 | 598 | last_AMT20_AB_read = AMT20_AB; |
timmey9 | 22:523e316cbe70 | 599 | } |
timmey9 | 22:523e316cbe70 | 600 | //current_sample_index = BEGIN_SAMPLING; // Used to force extra time. |
timmey9 | 22:523e316cbe70 | 601 | if (current_sample_index == WAITING_TO_BEGIN) {} |
timmey9 | 22:523e316cbe70 | 602 | else |
timmey9 | 22:523e316cbe70 | 603 | { |
timmey9 | 22:523e316cbe70 | 604 | if (current_sample_index == BEGIN_SAMPLING) { |
timmey9 | 22:523e316cbe70 | 605 | current_sample_index = FIRST_SAMPLE_INDEX; |
timmey9 | 22:523e316cbe70 | 606 | } |
timmey9 | 22:523e316cbe70 | 607 | |
timmey9 | 22:523e316cbe70 | 608 | sample_array[current_sample_index] = (A0_value << CHANNEL_STORAGE_OFFSET) | A2_value; |
timmey9 | 22:523e316cbe70 | 609 | |
timmey9 | 22:523e316cbe70 | 610 | angle_array[current_sample_index] = rotary_count; |
timmey9 | 22:523e316cbe70 | 611 | |
timmey9 | 22:523e316cbe70 | 612 | if (current_sample_index == LAST_SAMPLE_INDEX) { |
timmey9 | 22:523e316cbe70 | 613 | current_sample_index = WAITING_TO_BEGIN; |
timmey9 | 22:523e316cbe70 | 614 | //led_green = 1; |
timmey9 | 22:523e316cbe70 | 615 | } |
timmey9 | 22:523e316cbe70 | 616 | else { current_sample_index++; } |
timmey9 | 22:523e316cbe70 | 617 | } |
timmey9 | 22:523e316cbe70 | 618 | |
timmey9 | 23:9e5141647775 | 619 | //int tempVar = timeStamp.read_us(); |
timmey9 | 23:9e5141647775 | 620 | //timeStamp.stop(); |
timmey9 | 23:9e5141647775 | 621 | //timeStamp.reset(); |
timmey9 | 23:9e5141647775 | 622 | //pc.printf("TimeStamp: %i\r\n", tempVar); |
timmey9 | 22:523e316cbe70 | 623 | __enable_irq(); // Enable Interrupts |
timmey9 | 22:523e316cbe70 | 624 | } |
timmey9 | 22:523e316cbe70 | 625 | |
timmey9 | 22:523e316cbe70 | 626 | void analog_initialization(PinName pin) |
timmey9 | 22:523e316cbe70 | 627 | { |
timmey9 | 22:523e316cbe70 | 628 | ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
timmey9 | 22:523e316cbe70 | 629 | // MBED_ASSERT(adc != (ADCName)NC); |
timmey9 | 22:523e316cbe70 | 630 | |
timmey9 | 22:523e316cbe70 | 631 | uint32_t instance = adc >> ADC_INSTANCE_SHIFT; |
timmey9 | 22:523e316cbe70 | 632 | |
timmey9 | 22:523e316cbe70 | 633 | clock_manager_set_gate(kClockModuleADC, instance, true); |
timmey9 | 22:523e316cbe70 | 634 | |
timmey9 | 22:523e316cbe70 | 635 | uint32_t bus_clock; |
timmey9 | 22:523e316cbe70 | 636 | clock_manager_get_frequency(kBusClock, &bus_clock); |
timmey9 | 22:523e316cbe70 | 637 | uint32_t clkdiv; |
timmey9 | 22:523e316cbe70 | 638 | for (clkdiv = 0; clkdiv < 4; clkdiv++) { |
timmey9 | 22:523e316cbe70 | 639 | if ((bus_clock >> clkdiv) <= MAX_FADC) |
timmey9 | 22:523e316cbe70 | 640 | break; |
timmey9 | 22:523e316cbe70 | 641 | } |
timmey9 | 22:523e316cbe70 | 642 | if (clkdiv == 4) { |
timmey9 | 22:523e316cbe70 | 643 | clkdiv = 0x7; //Set max div |
timmey9 | 22:523e316cbe70 | 644 | } |
timmey9 | 22:523e316cbe70 | 645 | // adc is enabled/triggered when reading. |
timmey9 | 22:523e316cbe70 | 646 | adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2)); |
timmey9 | 22:523e316cbe70 | 647 | adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3)); |
timmey9 | 22:523e316cbe70 | 648 | adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref); |
timmey9 | 22:523e316cbe70 | 649 | adc_hal_set_resolution_mode(instance, kAdcSingleDiff16); |
timmey9 | 22:523e316cbe70 | 650 | adc_hal_configure_continuous_conversion(instance, false); |
timmey9 | 22:523e316cbe70 | 651 | adc_hal_configure_hw_trigger(instance, false); // sw trigger |
timmey9 | 22:523e316cbe70 | 652 | adc_hal_configure_hw_average(instance, false); |
timmey9 | 22:523e316cbe70 | 653 | adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4); |
timmey9 | 22:523e316cbe70 | 654 | adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail |
timmey9 | 22:523e316cbe70 | 655 | |
timmey9 | 22:523e316cbe70 | 656 | pinmap_pinout(pin, PinMap_ADC); |
timmey9 | 22:523e316cbe70 | 657 | } |
timmey9 | 22:523e316cbe70 | 658 | |
timmey9 | 22:523e316cbe70 | 659 | void output_data(uint32_t iteration_number) |
timmey9 | 22:523e316cbe70 | 660 | { |
timmey9 | 22:523e316cbe70 | 661 | pc.printf("Iteration: %i\n\r", iteration_number); |
timmey9 | 22:523e316cbe70 | 662 | pc.printf("Sampling rate: %i\n\r", SAMPLING_RATE); |
timmey9 | 22:523e316cbe70 | 663 | pc.printf("Data length: %i\n\r", TOTAL_SAMPLES); |
timmey9 | 22:523e316cbe70 | 664 | for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) { |
timmey9 | 22:523e316cbe70 | 665 | //pc.printf("%i\t%i\t%i\r\n", (sample_array[n] >> CHANNEL_STORAGE_OFFSET), (sample_array[n] & 0xFFFF), angle_array[n]); |
timmey9 | 22:523e316cbe70 | 666 | } |
timmey9 | 22:523e316cbe70 | 667 | |
timmey9 | 22:523e316cbe70 | 668 | } |