Seahorse IS/WAS
Produced: 7/12/2007 3:05:04 PM
   
Mode:  All Lines  
Left base folder: \\Tornado\ProjectLibrary\900403_AUV_GULPER\Software\SDF\Releases\EX_0.5  
Right base folder: \\Tornado\ProjectLibrary\900403_AUV_GULPER\Software\SDF\Releases\EX_0.4  
   
File: Gulper_Main.h  
1 /****************************************************************************/ = 1 /****************************************************************************/
2 /* Copyright 2006 MBARI.                                                    */   2 /* Copyright 2006 MBARI.                                                    */
3 /* Monterey Bay Aquarium Research Institute Proprietary Information.        */   3 /* Monterey Bay Aquarium Research Institute Proprietary Information.        */
4 /* All rights reserved.                                                     */   4 /* All rights reserved.                                                     */
5 /****************************************************************************/   5 /****************************************************************************/
6 #ifndef PH_PROBE_H   6 #ifndef PH_PROBE_H
7 #define PH_PROBE_H   7 #define PH_PROBE_H
8     8  
9  #define VS_VERSION  "0.5" <> 9  #define VS_VERSION  "0.4"
10 #define DEBUG 0 = 10 #define DEBUG 0
11     11  
12     12  
13 int8 fire[]="F";   13 int8 fire[]="F";
14 int8 query[]="Q";   14 int8 query[]="Q";
15 int8 status[]= "S";   15 int8 status[]= "S";
16 int8 version[]="V";   16 int8 version[]="V";
17 int8 actuation[]="A";   17 int8 actuation[]="A";
18 int8 reset[]="R";   18 int8 reset[]="R";
19     19  
20 char response_message[20]; // String for storing node response back to MVC   20 char response_message[20]; // String for storing node response back to MVC
21   <>    
22 int8 serial_delay_msec = 3; // delay for clocking 485 bits      
23   = 21  
24 // Timing variable initialization   22 // Timing variable initialization
25 int32 seconds = 0;                      // A running seconds counter for timer 1   23 int32 seconds = 0;                      // A running seconds counter for timer 1
26 int32 T2_seconds = 0;                   // A running seconds counter for timer 2   24 int32 T2_seconds = 0;                   // A running seconds counter for timer 2
27 int8 int_count = 0;                     // Number of interrupts left before a second has elapsed on timer 1   25 int8 int_count = 0;                     // Number of interrupts left before a second has elapsed on timer 1
28 int8 T2_int_count = 0;                  // Number of interrupts left before a second has elapsed on timer 2   26 int8 T2_int_count = 0;                  // Number of interrupts left before a second has elapsed on timer 2
29 int32 ElapsedSeconds = 0;               // A running seconds counter for timer 1   27 int32 ElapsedSeconds = 0;               // A running seconds counter for timer 1
30 int32 T2_ElapsedSeconds = 0;            // A running seconds counter for timer 2   28 int32 T2_ElapsedSeconds = 0;            // A running seconds counter for timer 2
31 int8 Elapsed_int_count = 0;             // Number of interrupts elapsed for timer 1   29 int8 Elapsed_int_count = 0;             // Number of interrupts elapsed for timer 1
32 int8 T2_Elapsed_int_count = 0;          // Number of interrupts elapsed for timer 2   30 int8 T2_Elapsed_int_count = 0;          // Number of interrupts elapsed for timer 2
33 int32 Final_Proximity_Milliseconds = 0; // Used to store time between fire and proximity sensor   31 int32 Final_Proximity_Milliseconds = 0; // Used to store time between fire and proximity sensor
34 int32 Final_Actuation_Milliseconds = 0; // Used to store time between fire and pin puller voltage >= 2 volts   32 int32 Final_Actuation_Milliseconds = 0; // Used to store time between fire and pin puller voltage >= 2 volts
35     33  
36 // Booleans   34 // Booleans
37 Boolean Fired = false;                // Fire command received?   35 Boolean Fired = false;                // Fire command received?
38 Boolean proximity = false;            // Proximity sensor ISR called?   36 Boolean proximity = false;            // Proximity sensor ISR called?
39 Boolean prox_error = false;            // Proximity sensor triggered before firing?   37 Boolean prox_error = false;            // Proximity sensor triggered before firing?
40 Boolean actuation_bool = false;       // Actuation voltage above 2 volts?   38 Boolean actuation_bool = false;       // Actuation voltage above 2 volts?
41 Boolean proximity_expired = false;    // Set true at 30 seconds past firing if no prox interrupt received   39 Boolean proximity_expired = false;    // Set true at 30 seconds past firing if no prox interrupt received
42 Boolean bad_address = false;          // Address out of range?   40 Boolean bad_address = false;          // Address out of range?
43 Boolean bad_checksum = false;         // Incoming checksum error?   41 Boolean bad_checksum = false;         // Incoming checksum error?
44 Boolean pin_puller_retracted = false; // did the pin puller retract?   42 Boolean pin_puller_retracted = false; // did the pin puller retract?
45     43  
46     44  
47 /*   45 /*
48     The ticks per second is based on an 8-bit counter over flowing that   46     The ticks per second is based on an 8-bit counter over flowing that
49     is driven by a 4 Mhz ocsillator prescaled by 4 and postscaled by 64.   47     is driven by a 4 Mhz ocsillator prescaled by 4 and postscaled by 64.
50     48  
51     TICKS_PER_SEC = (OSC_CLK) / (PRESCALE) / (POSTSCALE) / (8_BIT_OVERFLOW)   49     TICKS_PER_SEC = (OSC_CLK) / (PRESCALE) / (POSTSCALE) / (8_BIT_OVERFLOW)
52                   = 4000000   /     4      /      64     /      256   50                   = 4000000   /     4      /      64     /      256
53                   = 61.0351   51                   = 61.0351
54 */   52 */
55 #define TICKS_PER_SEC   61   53 #define TICKS_PER_SEC   61
56     54  
57 #define HI  1   55 #define HI  1
58 #define LO  0   56 #define LO  0
59     57  
60 #define ON  1   58 #define ON  1
61 #define OFF 0   59 #define OFF 0
62     60  
63 void startTimer(void);   61 void startTimer(void);
64 void getElapsedTime(void);   62 void getElapsedTime(void);
65 void T2_startTimer(void);   63 void T2_startTimer(void);
66 void T2_getElapsedTime(void);   64 void T2_getElapsedTime(void);
67     65  
68 #endif   66 #endif
   
File: RS485.c  
1 /****************************************************************************/ = 1 /****************************************************************************/
2 /* Copyright 2006 MBARI.                                                    */   2 /* Copyright 2006 MBARI.                                                    */
3 /* Monterey Bay Aquarium Research Institute Proprietary Information.        */   3 /* Monterey Bay Aquarium Research Institute Proprietary Information.        */
4 /* All rights reserved.                                                     */   4 /* All rights reserved.                                                     */
5 /****************************************************************************/   5 /****************************************************************************/
6 //   6 //
7 //   7 //
8 //   8 //
9 //   9 //
10 //   10 //
11 //   11 //
12 //   12 //
13     13  
14     14  
15 #include <stdlib.h>   15 #include <stdlib.h>
16     16  
17     17  
18 #ifndef RS485_DRIVER   18 #ifndef RS485_DRIVER
19 #define RS485_DRIVER   19 #define RS485_DRIVER
20     20  
21 int8 RS485_ID=0;   21 int8 RS485_ID=0;
22     22  
23     23  
24 #ifndef RS485_RX_PIN   24 #ifndef RS485_RX_PIN
25 #define RS485_RX_PIN       PIN_C7   // Data receive pin   25 #define RS485_RX_PIN       PIN_C7   // Data receive pin
26 #endif   26 #endif
27     27  
28 #ifndef RS485_TX_PIN   28 #ifndef RS485_TX_PIN
29 #define RS485_TX_PIN       PIN_C6   // Data transmit pin   29 #define RS485_TX_PIN       PIN_C6   // Data transmit pin
30 #endif   30 #endif
31     31  
32 #ifndef RS485_RX_ENABLE   32 #ifndef RS485_RX_ENABLE
33 #define RS485_RX_ENABLE    PIN_B5   // Controls RE pin.  Should keep low.   33 #define RS485_RX_ENABLE    PIN_B5   // Controls RE pin.  Should keep low.
34 #endif   34 #endif
35     35  
36 //***#use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN, enable=RS485_ENABLE_PIN,bits=8, stream=RS485,errors)//rcv   36 //***#use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN, enable=RS485_ENABLE_PIN,bits=8, stream=RS485,errors)//rcv
37 #use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN,bits=8, stream=RS485,errors, RESTART_WDT, DISABLE_INTS)//rcv   37 #use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN,bits=8, stream=RS485,errors, RESTART_WDT, DISABLE_INTS)//rcv
38     38  
39 #define RS485_wait_time 20             // Wait time in milliseconds   39 #define RS485_wait_time 20             // Wait time in milliseconds
40     40  
41 #bit    rs485_collision = rs232_errors.6   41 #bit    rs485_collision = rs232_errors.6
42     42  
43 //**#ifndef RS485_RX_BUFFER_SIZE   43 //**#ifndef RS485_RX_BUFFER_SIZE
44 //**#define RS485_RX_BUFFER_SIZE  32   44 //**#define RS485_RX_BUFFER_SIZE  32
45 //**#endif   45 //**#endif
46     46  
47 //**int rs485_state, rs485_ni, rs485_no,new_message;   47 //**int rs485_state, rs485_ni, rs485_no,new_message;
48 int new_message;   48 int new_message;
49 //**int8 rcv_state;   49 //**int8 rcv_state;
50     50  
51     51  
52 int8 rs485_InBuffer[30];   52 int8 rs485_InBuffer[30];
53 int8 cmd_InBuffer[1];   53 int8 cmd_InBuffer[1];
54 int index;   54 int index;
55 int Receiving = false;   55 int Receiving = false;
56 int8 to_address = 0xFF;        //Initialize to impossible address   56 int8 to_address = 0xFF;        //Initialize to impossible address
57 int8 incoming_checksum = 0x00; //To store incoming checksum   57 int8 incoming_checksum = 0x00; //To store incoming checksum
58     58  
59     59  
60 // Purpose:    Enable data reception   60 // Purpose:    Enable data reception
61 // Inputs:     None   61 // Inputs:     None
62 // Outputs:    None   62 // Outputs:    None
63 void RCV_ON(void) {   63 void RCV_ON(void) {
64       while(kbhit(RS485)) {fgetc(RS485);} // Clear RX buffer. Clear RDA interrupt flag. Clear overrun error flag.   64       while(kbhit(RS485)) {fgetc(RS485);} // Clear RX buffer. Clear RDA interrupt flag. Clear overrun error flag.
65          output_low(RS485_RX_ENABLE);   65          output_low(RS485_RX_ENABLE);
66 }   66 }
67     67  
68 void RCV_OFF(void) {   68 void RCV_OFF(void) {
69    output_high(RS485_RX_ENABLE);   69    output_high(RS485_RX_ENABLE);
70 }   70 }
71     71  
72     72  
73 // Purpose:    Initialize RS485 communication. Call this before   73 // Purpose:    Initialize RS485 communication. Call this before
74 //             using any other RS485 functions.   74 //             using any other RS485 functions.
75 // Inputs:     None   75 // Inputs:     None
76 // Outputs:    None   76 // Outputs:    None
77 void rs485_init() {   77 void rs485_init() {
78    RS485_ID= ( (!input(PIN_C3)<<3)|(!input(PIN_C2)<<2)|(!input(PIN_C1)<<1)|(!input(PIN_C0)) );   78    RS485_ID= ( (!input(PIN_C3)<<3)|(!input(PIN_C2)<<2)|(!input(PIN_C1)<<1)|(!input(PIN_C0)) );
79    RCV_ON();   79    RCV_ON();
80 //**   rs485_state=0;   80 //**   rs485_state=0;
81 //**   rs485_ni=0;   81 //**   rs485_ni=0;
82 //**   rs485_no=0;   82 //**   rs485_no=0;
83 //**   rcv_state=0;   83 //**   rcv_state=0;
84    new_message=FALSE;   84    new_message=FALSE;
85    enable_interrupts(INT_RDA);   85    enable_interrupts(INT_RDA);
86    enable_interrupts(GLOBAL);   86    enable_interrupts(GLOBAL);
87     87  
88 }   88 }
89     89  
90     90  
91 // Purpose:    Send a message over the RS485 bus   91 // Purpose:    Send a message over the RS485 bus
92 // Inputs:     1) To address   92 // Inputs:     1) To address
93 //             2) From address   93 //             2) From address
94 //             3) len   94 //             3) len
95 //             4) data (acknowlege, time to close)   95 //             4) data (acknowlege, time to close)
96 // Outputs:    TRUE if successful   96 // Outputs:    TRUE if successful
97 //             FALSE if failed   97 //             FALSE if failed
98 // Note:       Format:  msg typ | source | data   98 // Note:       Format:  msg typ | source | data
99 int1 rs485_send_message(int8 my_id, int8 len, int8* data) {   99 int1 rs485_send_message(int8 my_id, int8 len, int8* data) {
100    int8 try, i, cs;   100    int8 try, i, cs;
101    int1 ret = FALSE;   101    int1 ret = FALSE;
102     102  
103    RCV_OFF();   103    RCV_OFF();
104     104  
105    for(try=0; try<5; ++try) {   105    for(try=0; try<5; ++try) {
106       rs485_collision = 0;   106       rs485_collision = 0;
107     107  
108       for(i=0; i<len; ++i) {   108       for(i=0; i<len; ++i) {
109        //  cs ^= *data;   109        //  cs ^= *data;
110          fputc(*data, RS485);   110          fputc(*data, RS485);
111          ++data;   111          ++data;
112       }   112       }
113               113            
114       if(!rs485_collision) {   114       if(!rs485_collision) {
115       delay_ms(serial_delay_msec); <> 115       delay_ms(my_id);
116          ret = TRUE; = 116          ret = TRUE;
117          break;   117          break;
118       }   118       }
119       delay_ms(serial_delay_msec); <> 119       delay_ms(my_id);
120    } = 120    }
121    RCV_ON();   121    RCV_ON();
122    return(ret);   122    return(ret);
123 }   123 }
124     124  
125     125  
126     126  
127 // Purpose:    Interrupt service routine for handling incoming RS485 data   127 // Purpose:    Interrupt service routine for handling incoming RS485 data
128 // data format: from address, to address, length, command, data   128 // data format: from address, to address, length, command, data
129 #int_rda   129 #int_rda
130 void incomming_rs485()   130 void incomming_rs485()
131 {   131 {
132      rs485_InBuffer[index] = getc();   132      rs485_InBuffer[index] = getc();
133      if(rs485_InBuffer[index] == '$')   133      if(rs485_InBuffer[index] == '$')
134         {   134         {
135        Receiving = true;      135        Receiving = true;   
136        index = 0;   136        index = 0;
137        rs485_InBuffer[0]= '$';   137        rs485_InBuffer[0]= '$';
138       }   138       }
139        139     
140      if(Receiving)   140      if(Receiving)
141       {   141       {
142        if(index == 4)   142        if(index == 4)
143         {   143         {
144          cmd_InBuffer[index - 4] = rs485_InBuffer[index];   144          cmd_InBuffer[index - 4] = rs485_InBuffer[index];
145         }   145         }
146        index += 1;   146        index += 1;
147          147       
148        if(index >=7) // Check for end of message   148        if(index >=7) // Check for end of message
149         {   149         {
150          Receiving = false;  // Done receiving   150          Receiving = false;  // Done receiving
151          index = 0;          // Reset index   151          index = 0;          // Reset index
152          new_message=TRUE;   // Anncounce the receipt of a new message   152          new_message=TRUE;   // Anncounce the receipt of a new message
153     153  
154          //   154          //
155          // Convert ascii to hex for to_address   155          // Convert ascii to hex for to_address
156          //   156          //
157          if( (isxdigit(rs485_InBuffer[1])) && (isxdigit(rs485_InBuffer[2])) )   157          if( (isxdigit(rs485_InBuffer[1])) && (isxdigit(rs485_InBuffer[2])) )
158           {   158           {
159            if (rs485_InBuffer[2] > '9')   159            if (rs485_InBuffer[2] > '9')
160             {   160             {
161              rs485_InBuffer[2] += 9;   161              rs485_InBuffer[2] += 9;
162             }   162             }
163              rs485_InBuffer[2] &= 0x0F;   163              rs485_InBuffer[2] &= 0x0F;
164     164  
165             if (rs485_InBuffer[1] > '9')   165             if (rs485_InBuffer[1] > '9')
166              {   166              {
167               rs485_InBuffer[1] += 9;   167               rs485_InBuffer[1] += 9;
168              }   168              }
169               169            
170             rs485_InBuffer[1] &= 0x0F;   170             rs485_InBuffer[1] &= 0x0F;
171             to_address =( (rs485_InBuffer[1]<<1)|(rs485_InBuffer[2]) );           171             to_address =( (rs485_InBuffer[1]<<1)|(rs485_InBuffer[2]) );        
172           }   172           }
173          else   173          else
174           {   174           {
175            bad_address = true;      175            bad_address = true;   
176           }   176           }
177     177  
178          //          178          //       
179          // Convert ascii to hex for incoming_checksum   179          // Convert ascii to hex for incoming_checksum
180          //   180          //
181          if( (isxdigit(rs485_InBuffer[5])) && (isxdigit(rs485_InBuffer[6])) )   181          if( (isxdigit(rs485_InBuffer[5])) && (isxdigit(rs485_InBuffer[6])) )
182           {   182           {
183            if (rs485_InBuffer[6] > '9')   183            if (rs485_InBuffer[6] > '9')
184             {   184             {
185              rs485_InBuffer[6] += 9;   185              rs485_InBuffer[6] += 9;
186             }   186             }
187            rs485_InBuffer[6] &= 0x0F;   187            rs485_InBuffer[6] &= 0x0F;
188     188  
189            if (rs485_InBuffer[5] > '9')   189            if (rs485_InBuffer[5] > '9')
190             {   190             {
191              rs485_InBuffer[5] += 9;   191              rs485_InBuffer[5] += 9;
192             }   192             }
193             193          
194            rs485_InBuffer[5] &= 0x0F;   194            rs485_InBuffer[5] &= 0x0F;
195            incoming_checksum =( (rs485_InBuffer[5]<<1)|(rs485_InBuffer[6]) );           195            incoming_checksum =( (rs485_InBuffer[5]<<1)|(rs485_InBuffer[6]) );        
196           }   196           }
197          else   197          else
198           {   198           {
199            bad_checksum = true;      199            bad_checksum = true;   
200           }   200           }
201                   201                
202     202  
203         } // End of message processing   203         } // End of message processing
204       }   204       }
205      // Turn receive back on   205      // Turn receive back on
206     //** RCV_ON();   206     //** RCV_ON();
207 }   207 }
208     208  
209     209  
210 int1 rs485_get_new_message(int8 my_ID, int1 wait)   210 int1 rs485_get_new_message(int8 my_ID, int1 wait)
211 {   211 {
212    while(wait && (new_message==FALSE)) {}   212    while(wait && (new_message==FALSE)) {}
213     213  
214    if(new_message==FALSE)   214    if(new_message==FALSE)
215       return FALSE;   215       return FALSE;
216    else           216    else        
217     {   217     {
218       if( (to_address == RS485_ID) && (!bad_address) )      //check if message is for this address   218       if( (to_address == RS485_ID) && (!bad_address) )      //check if message is for this address
219       {   219       {
220       new_message=FALSE;                    //clear new_message flag   220       new_message=FALSE;                    //clear new_message flag
221       return TRUE;}   221       return TRUE;}
222       else  //message not for my address, clear new message flag but don't copy input data   222       else  //message not for my address, clear new message flag but don't copy input data
223       {   223       {
224       bad_address = false;   224       bad_address = false;
225       new_message=FALSE;   225       new_message=FALSE;
226       return FALSE;}   226       return FALSE;}
227    }   227    }
228 }   228 }
229     229  
230     230  
231     231  
232 // Purpose:    Wait for wait time for the RS485 bus to become idle   232 // Purpose:    Wait for wait time for the RS485 bus to become idle
233 // Inputs:     TRUE - restart the watch dog timer to prevent reset   233 // Inputs:     TRUE - restart the watch dog timer to prevent reset
234 //             FALSE - watch dog timer not restarted   234 //             FALSE - watch dog timer not restarted
235 // Outputs:    None   235 // Outputs:    None
236 void rs485_wait_for_bus(int1 clrwdt)   236 void rs485_wait_for_bus(int1 clrwdt)
237 {   237 {
238    int16 i;   238    int16 i;
239     239  
240    RCV_OFF();   240    RCV_OFF();
241    for(i=0; i <= (rs485_wait_time*20); ++i)   241    for(i=0; i <= (rs485_wait_time*20); ++i)
242    {   242    {
243       if(!input(RS485_RX_PIN))//this is inverted as in the original file   243       if(!input(RS485_RX_PIN))//this is inverted as in the original file
244          i = 0;   244          i = 0;
245       else   245       else
246          delay_us(50);   246          delay_us(50);
247     247  
248       if(clrwdt)   248       if(clrwdt)
249          restart_wdt();   249          restart_wdt();
250    }   250    }
251     RCV_ON();   251     RCV_ON();
252 }   252 }
253     253  
254 void send_response(char* s)   254 void send_response(char* s)
255 {   255 {
256    int8 size;   256    int8 size;
257    char* header[20];   257    char* header[20];
258 //    258 // 
259    output_bit(PIN_B1, HI); // Turn on TX LED   259    output_bit(PIN_B1, HI); // Turn on TX LED
260     260  
261    sprintf(header, "*%x",RS485_ID);   261    sprintf(header, "*%x",RS485_ID);
262    s = strncat(header, s, 16);   262    s = strncat(header, s, 16);
263     263  
264    for(size=0; s[size]!='\0'; ++size);   264    for(size=0; s[size]!='\0'; ++size);
265     265  
266    rs485_wait_for_bus(false);   266    rs485_wait_for_bus(false);
267    while(!rs485_send_message(RS485_ID,size, s))   267    while(!rs485_send_message(RS485_ID,size, s))
268    {      268    {   
269     delay_ms(10);   269     delay_ms(10);
270     }   270     }
271    output_bit(PIN_B1, LO); // Turn off TX LED   271    output_bit(PIN_B1, LO); // Turn off TX LED
272 }   272 }
273     273  
274 void send_error_response(char* s)   274 void send_error_response(char* s)
275 {   275 {
276    int8 size;   276    int8 size;
277    char* header[20];   277    char* header[20];
278    sprintf(header, "?%x ",RS485_ID);   278    sprintf(header, "?%x ",RS485_ID);
279    s = strncat(header, s, 16);   279    s = strncat(header, s, 16);
280     280  
281    for(size=0; s[size]!='\0'; ++size);   281    for(size=0; s[size]!='\0'; ++size);
282     282  
283    rs485_wait_for_bus(false);   283    rs485_wait_for_bus(false);
284    while(!rs485_send_message(RS485_ID,size, s))   284    while(!rs485_send_message(RS485_ID,size, s))
285    {      285    {   
286     delay_ms(10);   286     delay_ms(10);
287     }   287     }
288 }   288 }
289     289  
290 //#endif   290 //#endif