2 * C Implementation: main_test
8 * Author: Petr Kovacik <kovacp1@feld.cvut.cz>, (C) 2006
13 #define _USE_EXR_LEVELS 1
17 #include <periph/sci_rs232.h>
18 #include <system_def.h>
23 #include <pxmc_h2638.h>
31 /*struktury prikazu cmd*/
32 cmd_des_t const cmd_des_help={0, 0,"HELP","prints help for commands",
33 cmd_do_help,{(char*)&cmd_rs232}};
35 cmd_des_t const *cmd_rs232_default[]={
38 CMD_DES_CONTINUE_AT, /* list continues at new address */
39 (cmd_des_t*)cmd_pxmc_default,
42 cmd_des_t const **cmd_bth=cmd_rs232_default; /*cmd prikazy pro bth*/
43 cmd_des_t const **cmd_rs232=cmd_rs232_default; /*cmd prikazy pro PC*/
46 /*struktury charakterizujici motor 0*/
56 pxms_rs:0, //pxms_subdiv:8,
57 pxms_md:8000l<<8, pxms_ms:5000, pxms_ma:10,
58 pxms_inp_info:(long)TPU_TCNT1,//TPU_TCNT1 /*chanel TPU A,B*/
59 pxms_out_info:(long)PWM_PWBFR1A, /*chanel PWM A,B*/
60 pxms_ene:0, pxms_erc:0,
61 pxms_p:40, pxms_i:0, pxms_d:1, pxms_s1:0, pxms_s2:0,
62 pxms_me:0x1800, //6144
63 pxms_ptirc:40, // 2000 irc per rev, 200/4 steps /
67 pxms_cfg:PXMS_CFG_MD2E_m|PXMS_CFG_HLS_m|
68 PXMS_CFG_HPS_m|PXMS_CFG_HDIR_m|0x1
72 /*struktury charakterizujici motor 1*/
82 pxms_rs:0, //pxms_subdiv:8,
83 pxms_md:8000l<<8, pxms_ms:5000, pxms_ma:10,
84 pxms_inp_info:(long)TPU_TCNT2, /*chanel TPU C,D*/
85 pxms_out_info:(long)PWM_PWBFR1C, /*chanel PWM C,D*/
86 pxms_ene:0, pxms_erc:0,
87 pxms_p:40, pxms_i:0, pxms_d:1, pxms_s1:0, pxms_s2:0,
88 pxms_me:0x1800, //6144
89 pxms_ptirc:40, // 2000 irc per rev, 200/4 steps /
93 pxms_cfg:PXMS_CFG_MD2E_m|PXMS_CFG_HLS_m| //FIXME: nastavit spravne priznaky pro dalsi motorove struktur
94 PXMS_CFG_HPS_m|PXMS_CFG_HDIR_m|0x1
97 pxmc_state_t *pxmc_main_arr[] = {&mcsX0,&mcsX1};
99 pxmc_state_list_t pxmc_main_list = {
100 pxml_arr:pxmc_main_arr,
101 pxml_cnt:sizeof(pxmc_main_arr)/sizeof(pxmc_main_arr[0])
106 //*******************************************************
108 void unhandled_exception(void) __attribute__ ((interrupt_handler));
110 /*Interrupt routines*/
111 void unhandled_exception(void)
114 //********************************************************
117 int cmd_rs232_processor_run(void)
122 cmd_io=&cmd_io_rs232;
123 if(cmd_rs232_line_out(cmd_io))
126 if(cmd_rs232_line_in(cmd_io)<=0)
130 val=proc_cmd_line(cmd_io, cmd_rs232, cmd_io->priv.ed_line.in->buf);
135 if(cmd_io->priv.ed_line.out->inbuf){
136 cmd_io_putc(cmd_io,'\r');
137 cmd_io_putc(cmd_io,'\n');
141 cmd_io_write(&cmd_io_rs232,"ERROR ",6);
143 cmd_io_write(cmd_io,s,strlen(s));
144 cmd_io_putc(cmd_io,'\r');
145 cmd_io_putc(cmd_io,'\n');
150 extern void _print(char *str);
155 /********************************************************************************/
156 *DIO_PJDDR=0xff; /*output gate*/
157 *DIO_PEDDR=0xff; /*output gate*/
158 *DIO_PEDR=0x60; /*0x0-LED - light all; 0x6 -ENA,ENB=1, LE33CD=0*/
159 *DIO_PJDR=0x00; //rozsviceni vsech diod na */
162 /*priority preruseni - SCI > TPU*/
163 /* *SYS_SYSCR|=SYSCR_INTM1m; */
164 /* *INT_IPRA=0x22; *INT_IPRB=0x22; *INT_IPRC=0x04; */
165 /* *INT_IPRD=0x40; *INT_IPRE=0x44; *INT_IPRF=0x55; */
166 /* *INT_IPRG=0x55; *INT_IPRH=0x55; *INT_IPRJ=0x06; */
167 /* *INT_IPRK=0x67; *INT_IPRM=0x66; */
169 /*povoleni vsech preruseni atd...*/
171 excptvec_initfill(unhandled_exception, 0);
173 /*nastaveni seriovych linek - Bth, PC*/
174 //sci_rs232_setmode(RS232_BAUD_RAW | 3, 0, 0, 2); // HCI - hardcoded 115200
175 //sci_rs232_setmode(115200, 0, 0, 2); // HCI
176 sci_rs232_setmode(19200, 0, 0, sci_rs232_chan_default); //PC
181 /*nastaveni HW (TPU, PWM)*/
184 /*nastaveni DC motoru*/
185 pxmc_add_pservice_and_mode(4); /*Macro - mod=4 tj. all motors are DC*/
187 if (init_timer3((long long)200/*ms*/*1000*1000) < 0) {
194 // Distance of wheels - d
195 #define WHEEL_DIST 74 /* mm */
196 #define MAX_R 300 /* mm */
197 #define MIN_R (WHEEL_DIST/2) /* mm */
199 void move(int speed, int r)
206 sr = speed + (long long)speed * (d/2) / r;
207 sl = speed - (long long)speed * (d/2) / r;
212 pxmc_spd(&mcsX0, +sl, 0);
213 pxmc_spd(&mcsX1, -sr, 0);
214 //printf("speed=%5d, r=%5d, sl=%5d, sr=%5d\n", speed, r, sl, sr);
220 long int now = get_timer();
221 long int before = now;
226 cmd_rs232_processor_run(); /*sber + odesilani cmd prikazu PC*/
228 long int now = get_timer();
236 next = now + 1 + (rand() % 5);
237 //printf("%5ld->%5ld ", now, next);
238 if (rand() % 100 < 20)
239 speed = -speed; /* changing direction */
240 if (rand() % 100 < 50)
241 move(speed, 0); /* go stright ahaed */
243 radius = (rand() % (2*(MAX_R-MIN_R))) - (MAX_R-MIN_R);
244 if (radius >= 0) radius += MIN_R;
245 else radius -= MIN_R;
246 move(speed, radius); /* turn */
247 /* if (abs(radius) < WHEEL_DIST) */
248 /* next = now + 1;/\* sharp cuves are short *\/ */
258 /* TODO initialize rand accoring to voltage read from AD6. */