This is a test program for the LaOS - Laser Open Source driverboard, stepper drivers, sensors and I2C display.

Dependencies:   mbed SDFileSystem

Committer:
fablabtruck
Date:
Fri Jun 08 09:31:33 2012 +0000
Revision:
0:d4b6443a926f
svn rev 379

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fablabtruck 0:d4b6443a926f 1 /*
fablabtruck 0:d4b6443a926f 2 * main.cpp
fablabtruck 0:d4b6443a926f 3 *
fablabtruck 0:d4b6443a926f 4 * Copyright (c) 2011 Peter Brier
fablabtruck 0:d4b6443a926f 5 *
fablabtruck 0:d4b6443a926f 6 * This file is part of the LaOS project (see: http://laoslaser.org)
fablabtruck 0:d4b6443a926f 7 *
fablabtruck 0:d4b6443a926f 8 * LaOS is free software: you can redistribute it and/or modify
fablabtruck 0:d4b6443a926f 9 * it under the terms of the GNU General Public License as published by
fablabtruck 0:d4b6443a926f 10 * the Free Software Foundation, either version 3 of the License, or
fablabtruck 0:d4b6443a926f 11 * (at your option) any later version.
fablabtruck 0:d4b6443a926f 12 *
fablabtruck 0:d4b6443a926f 13 * LaOS is distributed in the hope that it will be useful,
fablabtruck 0:d4b6443a926f 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fablabtruck 0:d4b6443a926f 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fablabtruck 0:d4b6443a926f 16 * GNU General Public License for more details.
fablabtruck 0:d4b6443a926f 17 *
fablabtruck 0:d4b6443a926f 18 * You should have received a copy of the GNU General Public License
fablabtruck 0:d4b6443a926f 19 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
fablabtruck 0:d4b6443a926f 20 *
fablabtruck 0:d4b6443a926f 21 * This program tests the inputs and outputs of the laoslaser v1 board
fablabtruck 0:d4b6443a926f 22 * LED1 = xhome
fablabtruck 0:d4b6443a926f 23 * LED2 = yhome
fablabtruck 0:d4b6443a926f 24 * LED3 = zmin
fablabtruck 0:d4b6443a926f 25 * LED4 = zmax
fablabtruck 0:d4b6443a926f 26 *
fablabtruck 0:d4b6443a926f 27 */
fablabtruck 0:d4b6443a926f 28 #include "mbed.h"
fablabtruck 0:d4b6443a926f 29 #include "SDFileSystem.h"
fablabtruck 0:d4b6443a926f 30
fablabtruck 0:d4b6443a926f 31 // USB Serial
fablabtruck 0:d4b6443a926f 32 Serial pc(USBTX, USBRX); // tx, rx
fablabtruck 0:d4b6443a926f 33
fablabtruck 0:d4b6443a926f 34 // SD card
fablabtruck 0:d4b6443a926f 35 SDFileSystem sd(p11, p12, p13, p14, "sd");
fablabtruck 0:d4b6443a926f 36
fablabtruck 0:d4b6443a926f 37 // Analog in/out (cover sensor) + NC
fablabtruck 0:d4b6443a926f 38 DigitalIn cover(p19);
fablabtruck 0:d4b6443a926f 39
fablabtruck 0:d4b6443a926f 40 // I2C
fablabtruck 0:d4b6443a926f 41 I2C i2c(p9, p10); // sda, scl
fablabtruck 0:d4b6443a926f 42 #define _I2C_ADDRESS 0x04
fablabtruck 0:d4b6443a926f 43 #define _I2C_HOME 0xFE
fablabtruck 0:d4b6443a926f 44 #define _I2C_CLS 0xFF
fablabtruck 0:d4b6443a926f 45 #define _I2C_BAUD 10000
fablabtruck 0:d4b6443a926f 46
fablabtruck 0:d4b6443a926f 47 // status leds
fablabtruck 0:d4b6443a926f 48 DigitalOut led1(LED1);
fablabtruck 0:d4b6443a926f 49 DigitalOut led2(LED2);
fablabtruck 0:d4b6443a926f 50 DigitalOut led3(LED3);
fablabtruck 0:d4b6443a926f 51 DigitalOut led4(LED4);
fablabtruck 0:d4b6443a926f 52
fablabtruck 0:d4b6443a926f 53 // Inputs;
fablabtruck 0:d4b6443a926f 54 DigitalIn xhome(p8);
fablabtruck 0:d4b6443a926f 55 DigitalIn yhome(p17);
fablabtruck 0:d4b6443a926f 56 DigitalIn zmin(p15);
fablabtruck 0:d4b6443a926f 57 DigitalIn zmax(p16);
fablabtruck 0:d4b6443a926f 58
fablabtruck 0:d4b6443a926f 59
fablabtruck 0:d4b6443a926f 60 // motors
fablabtruck 0:d4b6443a926f 61 DigitalOut enable(p7);
fablabtruck 0:d4b6443a926f 62 DigitalOut xdir(p23);
fablabtruck 0:d4b6443a926f 63 DigitalOut xstep(p24);
fablabtruck 0:d4b6443a926f 64 DigitalOut ydir(p25);
fablabtruck 0:d4b6443a926f 65 DigitalOut ystep(p26);
fablabtruck 0:d4b6443a926f 66 DigitalOut zdir(p27);
fablabtruck 0:d4b6443a926f 67 DigitalOut zstep(p28);
fablabtruck 0:d4b6443a926f 68
fablabtruck 0:d4b6443a926f 69 // laser
fablabtruck 0:d4b6443a926f 70 DigitalOut o1(p22);
fablabtruck 0:d4b6443a926f 71 DigitalOut o2(p21);
fablabtruck 0:d4b6443a926f 72 DigitalOut o3(p6);
fablabtruck 0:d4b6443a926f 73 DigitalOut o4(p5);
fablabtruck 0:d4b6443a926f 74
fablabtruck 0:d4b6443a926f 75
fablabtruck 0:d4b6443a926f 76 // CAN bus
fablabtruck 0:d4b6443a926f 77 CAN can(p30, p29);
fablabtruck 0:d4b6443a926f 78
fablabtruck 0:d4b6443a926f 79
fablabtruck 0:d4b6443a926f 80 void home()
fablabtruck 0:d4b6443a926f 81 {
fablabtruck 0:d4b6443a926f 82 printf("Homing with x,y,z dir=0\n");
fablabtruck 0:d4b6443a926f 83 int i=0;
fablabtruck 0:d4b6443a926f 84 xdir = 0;
fablabtruck 0:d4b6443a926f 85 ydir = 0;
fablabtruck 0:d4b6443a926f 86 zdir = 0;
fablabtruck 0:d4b6443a926f 87 led1 = 0;
fablabtruck 0:d4b6443a926f 88 while ( 1 )
fablabtruck 0:d4b6443a926f 89 {
fablabtruck 0:d4b6443a926f 90 xstep = ystep = 0;
fablabtruck 0:d4b6443a926f 91 wait(0.0001);
fablabtruck 0:d4b6443a926f 92 xstep = xhome;
fablabtruck 0:d4b6443a926f 93 ystep = yhome;
fablabtruck 0:d4b6443a926f 94 zstep = zmin;
fablabtruck 0:d4b6443a926f 95 wait(0.0001);
fablabtruck 0:d4b6443a926f 96 led1 = !xhome;
fablabtruck 0:d4b6443a926f 97 led2 = !yhome;
fablabtruck 0:d4b6443a926f 98 led3 = !zmin;
fablabtruck 0:d4b6443a926f 99 led4 = ((i++) & 0x10000);
fablabtruck 0:d4b6443a926f 100 if ( (!xhome) && (!yhome) ) return;
fablabtruck 0:d4b6443a926f 101 }
fablabtruck 0:d4b6443a926f 102 }
fablabtruck 0:d4b6443a926f 103
fablabtruck 0:d4b6443a926f 104 #define TEST(c,x) case c: io = &x; x = !x; printf("Test %c: " #x " is now %s \n\r", c, (x ? "ON" : "OFF")); break
fablabtruck 0:d4b6443a926f 105
fablabtruck 0:d4b6443a926f 106 /**
fablabtruck 0:d4b6443a926f 107 *** main()
fablabtruck 0:d4b6443a926f 108 **/
fablabtruck 0:d4b6443a926f 109 int main()
fablabtruck 0:d4b6443a926f 110 {
fablabtruck 0:d4b6443a926f 111 led1 = led2 = led3 = led4 = 1;
fablabtruck 0:d4b6443a926f 112 Timer t;
fablabtruck 0:d4b6443a926f 113 DigitalOut *io = NULL;
fablabtruck 0:d4b6443a926f 114 CANMessage msg;
fablabtruck 0:d4b6443a926f 115
fablabtruck 0:d4b6443a926f 116 i2c.frequency(_I2C_BAUD);
fablabtruck 0:d4b6443a926f 117 pc.baud(115200);
fablabtruck 0:d4b6443a926f 118 //pc.baud(9600);
fablabtruck 0:d4b6443a926f 119
fablabtruck 0:d4b6443a926f 120 xhome.mode(PullUp);
fablabtruck 0:d4b6443a926f 121 yhome.mode(PullUp);
fablabtruck 0:d4b6443a926f 122 zmin.mode(PullUp);
fablabtruck 0:d4b6443a926f 123 zmax.mode(PullUp);
fablabtruck 0:d4b6443a926f 124
fablabtruck 0:d4b6443a926f 125 char buf[512];
fablabtruck 0:d4b6443a926f 126 char *help = "\n\r\n\r\n\rLaOS Test program\n\r"
fablabtruck 0:d4b6443a926f 127 __DATE__ " " __TIME__ "\n\r"
fablabtruck 0:d4b6443a926f 128 "++++++++++++++++++\n\r"
fablabtruck 0:d4b6443a926f 129 "Use these keys to test the functionality of the board:\n\r"
fablabtruck 0:d4b6443a926f 130 "xX: X Step/Dir\t"
fablabtruck 0:d4b6443a926f 131 "yY: Y Step/Dir\t"
fablabtruck 0:d4b6443a926f 132 "zZ: Z Step/Dir\t"
fablabtruck 0:d4b6443a926f 133 "tT: Ext Step/Dir (o1/o2)\n\r"
fablabtruck 0:d4b6443a926f 134 "e: Toggle Stepper enable\n\r"
fablabtruck 0:d4b6443a926f 135 "c: Can bus test\n\r"
fablabtruck 0:d4b6443a926f 136 "sS: SD-Card test (s=test file, S=speed test)\n\r"
fablabtruck 0:d4b6443a926f 137 "i: I2C\n\r"
fablabtruck 0:d4b6443a926f 138 "1/2/3/4: Toggle outputs\n\r"
fablabtruck 0:d4b6443a926f 139 "f: apply 1Khz frequency to last IO line (1 second)\n\r"
fablabtruck 0:d4b6443a926f 140 "h: X/Y homing (note: direction and home sensor polarity may be wrong\n\r";
fablabtruck 0:d4b6443a926f 141 char iline[] = " LAOS-IOTEST V1.1";
fablabtruck 0:d4b6443a926f 142 iline[0] = 0xFF;
fablabtruck 0:d4b6443a926f 143 //i2c.write(_I2C_ADDRESS, iline, 17);
fablabtruck 0:d4b6443a926f 144 printf(help);
fablabtruck 0:d4b6443a926f 145
fablabtruck 0:d4b6443a926f 146 int i=0;
fablabtruck 0:d4b6443a926f 147 int test=0;
fablabtruck 0:d4b6443a926f 148 char key=0;
fablabtruck 0:d4b6443a926f 149 while( 1 )
fablabtruck 0:d4b6443a926f 150 {
fablabtruck 0:d4b6443a926f 151 i++;
fablabtruck 0:d4b6443a926f 152 led1 = xhome;
fablabtruck 0:d4b6443a926f 153 led2 = yhome;
fablabtruck 0:d4b6443a926f 154 led3 = zmin;
fablabtruck 0:d4b6443a926f 155 led4 = zmax;
fablabtruck 0:d4b6443a926f 156 if ( pc.readable() )
fablabtruck 0:d4b6443a926f 157 test = pc.getc();
fablabtruck 0:d4b6443a926f 158 else
fablabtruck 0:d4b6443a926f 159 test = 0;
fablabtruck 0:d4b6443a926f 160 switch( test )
fablabtruck 0:d4b6443a926f 161 {
fablabtruck 0:d4b6443a926f 162 TEST('e',enable);
fablabtruck 0:d4b6443a926f 163 TEST('X',xdir);
fablabtruck 0:d4b6443a926f 164 TEST('x',xstep);
fablabtruck 0:d4b6443a926f 165 TEST('Y',ydir);
fablabtruck 0:d4b6443a926f 166 TEST('y',ystep);
fablabtruck 0:d4b6443a926f 167 TEST('Z',zdir);
fablabtruck 0:d4b6443a926f 168 TEST('z',zstep);
fablabtruck 0:d4b6443a926f 169 TEST('t',o2);
fablabtruck 0:d4b6443a926f 170 TEST('T',o1);
fablabtruck 0:d4b6443a926f 171 TEST('1',o1);
fablabtruck 0:d4b6443a926f 172 TEST('2',o2);
fablabtruck 0:d4b6443a926f 173 TEST('3',o3);
fablabtruck 0:d4b6443a926f 174 TEST('4',o4);
fablabtruck 0:d4b6443a926f 175 case 'h':
fablabtruck 0:d4b6443a926f 176 home();
fablabtruck 0:d4b6443a926f 177 break;
fablabtruck 0:d4b6443a926f 178 case 'f':
fablabtruck 0:d4b6443a926f 179 case 'F':
fablabtruck 0:d4b6443a926f 180 if (io != NULL )
fablabtruck 0:d4b6443a926f 181 {
fablabtruck 0:d4b6443a926f 182 printf("pulsing output for 1000 pulses sec at 500Hz or 10kHz...\n");
fablabtruck 0:d4b6443a926f 183 for(i=0;i<1000;i++)
fablabtruck 0:d4b6443a926f 184 {
fablabtruck 0:d4b6443a926f 185 wait((test == 'f' ? 0.001 : 0.00005));
fablabtruck 0:d4b6443a926f 186 *io = 1;
fablabtruck 0:d4b6443a926f 187 wait((test == 'f' ? 0.001 : 0.00005));
fablabtruck 0:d4b6443a926f 188 *io = 0;
fablabtruck 0:d4b6443a926f 189 }
fablabtruck 0:d4b6443a926f 190
fablabtruck 0:d4b6443a926f 191 }
fablabtruck 0:d4b6443a926f 192 break;
fablabtruck 0:d4b6443a926f 193 case 's':
fablabtruck 0:d4b6443a926f 194 memset(buf,0,sizeof(buf));
fablabtruck 0:d4b6443a926f 195 printf("Testing IO board SD card: Write...\n\r");
fablabtruck 0:d4b6443a926f 196 FILE *test1 = fopen("/sd/text.txt", "wb");
fablabtruck 0:d4b6443a926f 197 fwrite("bla bla\n",8, 1, test1);
fablabtruck 0:d4b6443a926f 198 fclose(test1);
fablabtruck 0:d4b6443a926f 199 printf("Testing IO board SD card: Read...\n\r");
fablabtruck 0:d4b6443a926f 200 test1 = fopen("/sd/text.txt", "rb");
fablabtruck 0:d4b6443a926f 201 fread(&buf,8, 1, test1);
fablabtruck 0:d4b6443a926f 202 printf("Result: '%s'\n\r", buf);
fablabtruck 0:d4b6443a926f 203 fclose(test1);
fablabtruck 0:d4b6443a926f 204 break;
fablabtruck 0:d4b6443a926f 205 case 'S':
fablabtruck 0:d4b6443a926f 206 printf("SD card speed test (writing and reading 1MByte file\n");
fablabtruck 0:d4b6443a926f 207 memset(buf,23,sizeof(buf));
fablabtruck 0:d4b6443a926f 208 FILE *test2 = fopen("/sd/test.bin", "wb");
fablabtruck 0:d4b6443a926f 209 t.reset();
fablabtruck 0:d4b6443a926f 210 t.start();
fablabtruck 0:d4b6443a926f 211 for(i=0;i<sizeof(buf);i++)
fablabtruck 0:d4b6443a926f 212 buf[i] = (char)t.read_ms();
fablabtruck 0:d4b6443a926f 213 t.stop(); t.reset(); t.start();
fablabtruck 0:d4b6443a926f 214 for(i=0;i<200;i++)
fablabtruck 0:d4b6443a926f 215 fwrite(buf,sizeof(buf), 1, test2);
fablabtruck 0:d4b6443a926f 216 t.stop();
fablabtruck 0:d4b6443a926f 217 fclose(test2);
fablabtruck 0:d4b6443a926f 218 printf("Write 1MByte: %d msec\n\r", (int)t.read_ms());
fablabtruck 0:d4b6443a926f 219 test2 = fopen("/sd/test.bin", "rb");
fablabtruck 0:d4b6443a926f 220 t.reset();
fablabtruck 0:d4b6443a926f 221 t.start();
fablabtruck 0:d4b6443a926f 222 while( fread(&buf, sizeof(buf), 1, test2) > 0 );
fablabtruck 0:d4b6443a926f 223 t.stop();
fablabtruck 0:d4b6443a926f 224 printf("Read 1MByte: %d msec\n\r", (int)t.read_ms());
fablabtruck 0:d4b6443a926f 225 case 'i':
fablabtruck 0:d4b6443a926f 226 printf("I2C test:\n\r");
fablabtruck 0:d4b6443a926f 227 char s[48];
fablabtruck 0:d4b6443a926f 228 sprintf(s," 0123456789ABCDEF1234567890ABCDEF");
fablabtruck 0:d4b6443a926f 229 s[0] = 0xFF;
fablabtruck 0:d4b6443a926f 230 i2c.write(_I2C_ADDRESS, s, strlen(s));
fablabtruck 0:d4b6443a926f 231 wait(3);
fablabtruck 0:d4b6443a926f 232 i2c.read(_I2C_ADDRESS, s, 48);
fablabtruck 0:d4b6443a926f 233 sprintf(iline," Key read: %c", s[0]);
fablabtruck 0:d4b6443a926f 234 i2c.write(_I2C_ADDRESS, iline, strlen(iline));
fablabtruck 0:d4b6443a926f 235 break;
fablabtruck 0:d4b6443a926f 236 case 'c':
fablabtruck 0:d4b6443a926f 237 printf("CAN bus test, sending and receiving characters, press any key to quit\n\r");
fablabtruck 0:d4b6443a926f 238 can.frequency(500000);
fablabtruck 0:d4b6443a926f 239 can.reset();
fablabtruck 0:d4b6443a926f 240 t.start();
fablabtruck 0:d4b6443a926f 241 can.reset();
fablabtruck 0:d4b6443a926f 242 while( !pc.readable() )
fablabtruck 0:d4b6443a926f 243 {
fablabtruck 0:d4b6443a926f 244 if ( t > 1.0 )
fablabtruck 0:d4b6443a926f 245 {
fablabtruck 0:d4b6443a926f 246 t.reset();
fablabtruck 0:d4b6443a926f 247 can.write(CANMessage(10, &key, 1));
fablabtruck 0:d4b6443a926f 248 key++;
fablabtruck 0:d4b6443a926f 249 }
fablabtruck 0:d4b6443a926f 250 wait(0.1);
fablabtruck 0:d4b6443a926f 251 if(can.read(msg))
fablabtruck 0:d4b6443a926f 252 printf("Message received: id=%d, len=%d, data[0]=%d\n\n\r", (int)msg.id, (int)msg.len, (int)msg.data[0]);
fablabtruck 0:d4b6443a926f 253
fablabtruck 0:d4b6443a926f 254 printf("value=%d, rderror=%d, tderror=%d\n\r", (int)key, can.rderror(), can.tderror());
fablabtruck 0:d4b6443a926f 255
fablabtruck 0:d4b6443a926f 256 }
fablabtruck 0:d4b6443a926f 257 break;
fablabtruck 0:d4b6443a926f 258 case 0:
fablabtruck 0:d4b6443a926f 259 case '\n':
fablabtruck 0:d4b6443a926f 260 case '\r': break;
fablabtruck 0:d4b6443a926f 261 default: printf(help); break;
fablabtruck 0:d4b6443a926f 262 }
fablabtruck 0:d4b6443a926f 263 //i2c.read(_I2C_ADDRESS,&key, 1);
fablabtruck 0:d4b6443a926f 264 if (key != 0) {
fablabtruck 0:d4b6443a926f 265 char s[48];
fablabtruck 0:d4b6443a926f 266 printf("I2C key read: %c\n\r", key);
fablabtruck 0:d4b6443a926f 267 sprintf(s," Key read: %c", key);
fablabtruck 0:d4b6443a926f 268 s[0] = 0xFF;
fablabtruck 0:d4b6443a926f 269 //i2c.write(_I2C_ADDRESS, s, strlen(s));
fablabtruck 0:d4b6443a926f 270 wait(0.2);
fablabtruck 0:d4b6443a926f 271 if (key == '7') {
fablabtruck 0:d4b6443a926f 272 wait(1);
fablabtruck 0:d4b6443a926f 273 //i2c.write(_I2C_ADDRESS, iline, 17);
fablabtruck 0:d4b6443a926f 274 }
fablabtruck 0:d4b6443a926f 275 //i2c.read(_I2C_ADDRESS, s, 48);
fablabtruck 0:d4b6443a926f 276 key=0;
fablabtruck 0:d4b6443a926f 277 }
fablabtruck 0:d4b6443a926f 278 if ( test )
fablabtruck 0:d4b6443a926f 279 {
fablabtruck 0:d4b6443a926f 280 printf("xhome=%d, yhome=%d, zmin=%d, zmax=%d, cover=%s\n\r", (int)xhome, (int)yhome, (int)zmin, (int)zmax, (cover ? "OPEN" : "CLOSED") );
fablabtruck 0:d4b6443a926f 281 }
fablabtruck 0:d4b6443a926f 282 }
fablabtruck 0:d4b6443a926f 283 }