main.c

Go to the documentation of this file.
00001 
00011 #include "t89c51cc02.h"
00012 #include "datatypes.h"
00013 #include "adc.h"
00014 #include "can.h"
00015 #include "timer.h"
00016 #include "sharp.h"
00017 #include "fingersensors.h"
00018 #include "photosensor.h"
00019 #include "bumper.h"
00020 #include "command.h"
00021 #include "eeprom.h"
00022 
00024 #define DEBUG 1
00025 
00027 #define LENGTH_SENSORBUFFER 128
00028 
00030 /* #define SAMPLEPOINTS BUMPERSENSORS_NR + PHOTOSENSORS_NR + FINGERSENSORS_NR + 1 */
00031 #define SAMPLEPOINTS 18
00032 
00034 #define BUFFER_LENGTH 6 * SAMPLEPOINTS
00035 
00037 #define SERIALNUMBER 0x01
00038 
00040 const char version[]="Sensor Control 07092007 #1";
00041 
00048 WORD xdata sensorbuffer[BUFFER_LENGTH];
00049 
00053 void main()
00054 {
00055         BYTE i;
00056         BYTE bufferptr; /* index pointer into the sensorbuffer */
00057         WORD samplenr; /* counts number of sensor sampling runs */
00058         unsigned long read_shiftreg; /* a shift register which contains the bit pattern for all sensors with scheduled readings */
00059 
00060         P1=0; P3=0; P2=0;
00061         samplenr=0;
00062         bufferptr=0;
00063         for(i=0;i<BUFFER_LENGTH;i++)
00064                 sensorbuffer[i]=0;
00065 
00066         /* init functions - prepare but don't enable interrupts */
00067         Command_ReadDefaultConfiguration();
00068         ADC_init();
00069         CAN_init();
00070         Timer_init();
00071         Bumper_init();
00072         Sharp_init();
00073         Photosensor_init();
00074         Fingersensors_init();
00075 
00076         EA=1;           /* enable all interrupts */
00077 
00078         /* send initial hello World */
00079         CAN_setchannel(1);
00080         CANCONCH = CH_DISABLE;                    /* reset channel 1 configuration */            
00081         CANMSG = 'S';
00082         CANMSG = 'C';
00083         CANMSG = '0'+SERIALNUMBER;      
00084         CANCONCH |= 3;              
00085         CANCONCH |= CH_TxENA;                     /* emission enabled */
00086         CAN_enablechannel(1);                       /* channel 1 enable */
00087         CANSTCH=0x00; 
00088 
00089         Command_TimecheckEnable();
00090         read_eeprom_config=0;
00091 
00092         /* main loop - process all tasks here */
00093         while(1)
00094         {
00095         
00096                 /* pending measurement task? */
00097                 if(measurement_task)
00098                 {
00099                         measurement_task=0;     /* acknowledge measurement task */
00100                         /* sample upper photosensor of finger */
00101                         sensorbuffer[bufferptr]=Photosensor_read(1);
00102                         /* sample finger sensors */                     
00103                         Fingersensors_read();
00104                         for(i=0;i<FINGERSENSORS_NR;i++)
00105                                 sensorbuffer[bufferptr+14+i]=fingersensors[i];
00106                         /* sample lower photosensor of finger */
00107                         sensorbuffer[bufferptr+1]=Photosensor_read(2);
00108                         Photosensor_PowerOff();
00109                         /* begin sampling Sharp sensor */
00110                         Sharp_PowerOn();
00111                         /* we have approximately 50ms until the Sharp is ready - sample the bumper sensors
00112                          * while we wait for the Sharp sensor to be ready
00113                          */
00114                         for(i=0;i<BUMPERSENSORS_NR;i++) 
00115                         {
00116                                 sensorbuffer[bufferptr+6+i]=Bumper_read(i);
00117                                 
00118                         }
00119                         /* read the Sharp sensor */
00120                         sensorbuffer[bufferptr+5]=Sharp_read();
00121                         Sharp_PowerOff();
00122         
00123                         /* sample middle photo sensor */        
00124                         sensorbuffer[bufferptr+2]=Photosensor_read(3);
00125 
00126                         /* handle communication tasks if requested */
00127                         if(reporting)
00128                         {
00129                                 if(Command_ReportDue()) /* only report when due */
00130                                         for(i=0;i<SAMPLEPOINTS;i++)
00131                                         {
00132                                                 ECAN=0; /* disable CAN interrupt to prevent corruption of message buffer */
00133                                                 can_data[0]='S';
00134                                                 can_data[1]=i;
00135                                                 can_data[2]=(BYTE) ((sensorbuffer[bufferptr+i] & 0xff00) >> 8);
00136                                                 can_data[3]=(BYTE) (sensorbuffer[bufferptr+i] & 0x00ff);
00137                                                 CAN_SendMsg(4);
00138                                         }
00139                         } else {
00140                                 /* read and watch requests are handled here */
00141                                 read_shiftreg=readenable;
00142                                 for(i=0;i<SAMPLEPOINTS;i++)
00143                                 {                       
00144                                                 /* read requests overrule watch requests */             
00145                                                 if(read_shiftreg & 0x1)
00146                                                 {
00147                                                         ECAN=0; /* disable CAN interrupt to prevent corruption of message buffer */
00148                                                         can_data[0]='S';
00149                                                         can_data[1]=i;
00150                                                         can_data[2]=(BYTE) ((sensorbuffer[bufferptr+i] & 0xff00) >> 8);
00151                                                         can_data[3]=(BYTE) (sensorbuffer[bufferptr+i] & 0x00ff);
00152                                                         CAN_SendMsg(4);
00153                                                 } else {
00154                                                         /* check for monitored sensor value */
00155                                                         if(CheckMonitor(i,sensorbuffer[bufferptr+i]))
00156                                                         {
00157                                                                 ECAN=0; /* disable CAN interrupt to prevent corruption of message buffer */
00158                                                                 can_data[0]='M';
00159                                                                 can_data[1]=i;
00160                                                                 can_data[2]=(BYTE) ((sensorbuffer[bufferptr+i] & 0xff00) >> 8);
00161                                                                 can_data[3]=(BYTE) (sensorbuffer[bufferptr+i] & 0x00ff);
00162                                                                 CAN_SendMsg(4);
00163 
00164                                                         }
00165                                                 }
00166                                                 read_shiftreg=(read_shiftreg >> 1); /* cycle next bit */
00167                                 }
00168                                 readenable=0;   /* make sure read requests for single sensor occur once */
00169 
00170                         }
00171 
00172                         bufferptr += SAMPLEPOINTS;
00173                         /* loop bufferptr if needed */
00174                         if(bufferptr>BUFFER_LENGTH)
00175                                 bufferptr=0;
00176 
00177                         samplenr++;     /* count the number of measurements taken */
00178                         /* report miss of time constraints if requested by the user */
00179                         if(measurement_task && Command_CheckTime())
00180                         {
00181                                 /* error - time constraint for measurement task was not met */
00182                                 ECAN=0; /* disable CAN interrupt to prevent corruption of message buffer */
00183                                 can_data[0]='T';
00184                                 can_data[1]=CAN_NAK;
00185                                 CAN_SendMsg(2);
00186                         }
00187                         measurement_task=0;
00188                 }
00189         
00190                 /* pending task to reinitialize the monitor points from EEPROM data? */
00191                 if(read_eeprom_config)
00192                 {
00193                         EA=0;
00194                         Command_ReadDefaultConfiguration();
00195                         EA=1;
00196 #ifdef DEBUG
00197                                 ECAN=0;
00198                                 can_data[0]='E';
00199                                 can_data[1]='P';
00200                                 can_data[2]='R';
00201                                 CAN_SendMsg(3);
00202 #endif
00203                         read_eeprom_config=0;
00204                         write_eeprom_config=0; /* it makes no sense to schedule both read and write in one timeframe, we therefor disable any accidentially scheduled write task */
00205                 }
00206 
00207                 /* pending task to write monitor settings to EEPROM? */
00208                 if(write_eeprom_config)
00209                 {
00210                         Command_WriteDefaultConfiguration();
00211 #ifdef DEBUG
00212                         ECAN=0;
00213                         can_data[0]='E';
00214                         can_data[1]='P';
00215                         can_data[2]='R';
00216                         can_data[3]='w';
00217                         CAN_SendMsg(4);
00218 #endif
00219                         write_eeprom_config=0;
00220                 }
00221 
00222         }
00223 }

Generated on Wed Oct 24 12:57:24 2007 for SensorControl by  doxygen 1.4.6