xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
hardwareTest.c
1 /*
2  * hardwareTest.c
3  *
4  * Copyright (c) 2013, Thomas Buck <xythobuz@me.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 #include <avr/io.h>
35 #include <avr/pgmspace.h>
36 
37 #include <tasks.h>
38 #include <xycontrol.h>
39 #include <time.h>
40 #include <uartMenu.h>
41 #include <serial.h>
42 #include <acc.h>
43 #include <gyro.h>
44 #include <mag.h>
45 #include <motor.h>
46 #include <orientation.h>
47 #include <xmem.h>
48 #include <error.h>
49 
55 void ledTask(void);
56 void printVoltage(void);
57 void printRaw(void);
58 void ramTest(void);
59 void bluetoothTest(void);
60 
61 /*
62  * Strings for UART menu, stored in Flash.
63  */
64 char PROGMEM voltageString[] = "Battery Voltage";
65 char PROGMEM sensorString[] = "Raw Sensor Data";
66 char PROGMEM ramString[] = "Test external RAM";
67 char PROGMEM bluetoothString[] = "Test Bluetooth Module";
68 
69 int main(void) {
70 
71  /*
72  * Initialize the System Timer, UART, TWI, SPI,
73  * ADC and the UART menu task for user or software
74  * interaction. Also enables interrupts!
75  * Also, the UART will be tied to stdin, stdout and stderr.
76  * This allows you to use stdio.h utilities like printf()
77  */
78  xyInit();
79  printf("Initializing Hardware Test...\n");
80 
81  /*
82  * Initialize Hardware
83  */
86  motorInit();
88 
89  /*
90  * Register Tasks in the Scheduler. A UART task
91  * is already registered...
92  */
93  addTask(&ledTask); // Blink LED
94 
95  /*
96  * Add commands for the UART menu
97  */
98  addMenuCommand('b', bluetoothString, &bluetoothTest);
99  addMenuCommand('r', sensorString, &printRaw);
100  addMenuCommand('t', ramString, &ramTest);
101  addMenuCommand('v', voltageString, &printVoltage);
102 
103  printf("Hardware Test Initialized!\n");
104 
105  /*
106  * Execute all registered tasks, forever.
107  */
108  for(;;) {
109  tasks();
110  }
111 
112  return 0;
113 }
114 
115 void ledTask(void) {
116  /*
117  * Basic example of executing a task with a given frequency.
118  * last contains the last time this task was executed.
119  */
120  static time_t last = 0;
121  if ((getSystemTime() - last) > 125) { // 125ms have passed
122  xyLed(LED_ALL, LED_TOGGLE); // Do something...
123  last = getSystemTime(); // Store new execution time
124  }
125 }
126 
127 void printVoltage(void) {
128  printf("Battery: %fV\n", getVoltage());
129 }
130 
131 void printRaw(void) {
132  Vector3f v;
133  accRead(&v);
134  printf("Ax: %f Ay: %f Az: %f\n", v.x, v.y, v.z);
135  gyroRead(&v);
136  printf("Gx: %f Gy: %f Gz: %f\n", v.x, v.y, v.z);
137  magRead(&v);
138  printf("Mx: %f My: %f Mz: %f\n", v.x, v.y, v.z);
139 }
140 
141 #define CHECKSIZE 53248 // 52KB
142 
143 void ramTest(void) {
144  uint8_t *blocks[MEMBANKS];
145  uint8_t oldBank = xmemGetBank();
146 
147  printf("Allocating Test Memory...\n");
148  for (uint8_t i = 0; i < MEMBANKS; i++) {
149  xmemSetBank(i);
150  blocks[i] = (uint8_t *)malloc(CHECKSIZE);
151  if (blocks[i] == NULL) {
152  printf(" Error: Couldn't allocate %liKB in Bank %i!\n", (CHECKSIZE / 1024), i);
153  } else {
154  printf(" Bank %i ready!\n", i);
155  }
156  }
157  printf("Filling with data...\n");
158  for (uint8_t i = 0; i < MEMBANKS; i++) {
159  xmemSetBank(i);
160  for (uint16_t j = 0; j < CHECKSIZE; j++) {
161  blocks[i][j] = (j & 0xFF);
162  }
163  printf(" Filled Bank %i!\n", i);
164  }
165  printf("Checking data...\n");
166  for (uint8_t i = 0; i < MEMBANKS; i++) {
167  xmemSetBank(i);
168  uint8_t error = 0;
169  for (uint16_t j = 0; ((j < CHECKSIZE) && (!error)); j++) {
170  if (blocks[i][j] != (j & 0xFF)) {
171  printf(" Error at %i in %i!\n", j, i);
172  error = 1;
173  }
174  }
175  if (!error) {
176  printf(" Bank %i okay!\n", i);
177  }
178  }
179  printf("Freeing memory...\n");
180  for (uint8_t i = 0; i < MEMBANKS; i++) {
181  xmemSetBank(i);
182  free(blocks[i]);
183  }
184  printf("Finished!\n");
185 
186  xmemSetBank(oldBank);
187 }
188 
189 void bluetoothTest(void) {
190  printf("Please disconnect, wait 10s, then reconnect!\n");
191  printf("All data will be logged, then printed after 15s.\n");
192  time_t start = getSystemTime();
193  while ((getSystemTime() - start) <= 15000); // Wait
194  while (serialHasChar(BLUETOOTH)) { // Check
196  }
197  printf("\n\nDone!\n");
198 }