Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / homing stop / stop.c @ 1390

History | View | Annotate | Download (3.22 KB)

1 80 bneuman
#include<avr/io.h>
2 660 bneuman
#include <avr/interrupt.h>
3 80 bneuman
#include "i2c.h"
4
5 703 bneuman
#define LEDG PB4 //Green
6
#define LEDR PB5 //Red
7 80 bneuman
8 660 bneuman
// for i2c_byte coming from charge board
9
//I2C Message Codes
10
#define I2C_MSG_ACKNOWLEDGE       'A'
11
#define I2C_MSG_BATTERY_CHARGING  'C'
12
#define I2C_MSG_DATA              'D'
13
#define I2C_MSG_CONTACT_ERROR     'E'
14
#define I2C_MSG_BATTERY_FULL      'F'
15
#define I2C_MSG_NO_CONTACT        'N'
16
#define I2C_MSG_REQUEST_DATA      'R'
17
#define I2C_MSG_GO_TO_SLEEP       'Y'
18
#define I2C_MSG_ENTERING_SLEEP    'Z'
19
#define I2C_MSG_HOMING            'H'
20
21 663 bneuman
#define DETECT_12V PB6
22 660 bneuman
23 663 bneuman
24 660 bneuman
#define SW0 PA6
25
#define HOMING_PIN PA7
26
27
uint8_t read_homing(void)
28
{
29
    uint8_t ret = PINA & _BV(HOMING_PIN);
30
    if(ret)
31
        PORTA |= _BV(PA3);
32
    else
33
        PORTA &= ~_BV(PA3);
34
    return ret;
35
}
36
37
uint8_t get_delay(void)
38
{
39
    uint8_t count = 0;
40
41 703 bneuman
        PORTB|=_BV(LEDR);
42 660 bneuman
    while(read_homing())
43
    {
44
        delay_ms(1);
45
        count++;
46
47
        if (count >= 100)
48
            return 1;
49
    } //wait a beacon cycle to make sure we aren't starting the count in the middle of one
50 703 bneuman
        PORTB &= ~_BV(LEDR);
51 660 bneuman
    count = 0;
52 703 bneuman
    PORTB |= _BV(LEDG);
53 660 bneuman
    while(!read_homing())
54
    {
55
        delay_ms(1);
56
        count++;
57
        if(count==255)
58
          return 2;
59
    }
60 703 bneuman
    PORTB &= ~_BV(LEDG);
61 660 bneuman
62
  /*RECH_PUTS("\n\rCount: ");
63
        RECH_PUTI(count);
64
        RECH_PUTC('.');*/
65
66
    return count;
67
}
68
69 80 bneuman
int main( void )
70
{
71 703 bneuman
    uint8_t count;
72 663 bneuman
73 703 bneuman
74
        DDRB |= /*_BV(PB0) |*/ _BV(LEDR) | _BV(LEDG); //what is PB0 for?
75
    DDRB &= ~_BV(DETECT_12V);
76
77
    i2c_init();
78
79 80 bneuman
80
  /*tempData[0] = 'C';
81
                tempData[1] = abs_time>>8;
82
                tempData[2] = abs_time&0xFF;
83
                i2c_putpacket(0x01, tempData, 3);*/
84
85 703 bneuman
  char data[4];
86 80 bneuman
  data[0]=I2C_MSG_DATA;
87 703 bneuman
88 663 bneuman
89
90 80 bneuman
  while(1)
91
  {
92 703 bneuman
93
    PORTB |=_BV(LEDG);
94
    PORTB |=_BV(LEDR);
95
    delay_ms(500);
96
    PORTB &= ~_BV(LEDG);
97
    PORTB &= ~_BV(LEDR);
98
99
100 80 bneuman
    while(1)
101
    {
102 703 bneuman
103
      if((PINB & _BV(DETECT_12V)))
104 80 bneuman
      {
105 703 bneuman
        PORTB |= _BV(LEDG);
106
107 80 bneuman
        //This is a junk byte. For some reason the first packet is always ignored????
108
        data[1] = 'a';
109
        i2c_putpacket(0x01, data, 2);
110
        data[1]=I2C_MSG_BATTERY_CHARGING;
111
        i2c_putpacket(0x01, data, 2);
112
        break;
113
      }
114
      else
115
      {
116 703 bneuman
        count = 0;
117 663 bneuman
        data[1]=I2C_MSG_NO_CONTACT;
118 660 bneuman
119
        get_delay(); //reject the first reading //homing import stuff, uncomment this!!!!!!
120 663 bneuman
        count = get_delay();
121
        if(count>2){
122
            data[2]=I2C_MSG_HOMING;
123 703 bneuman
            data[3]= count;
124
125 663 bneuman
            i2c_putpacket(0x01, data, 4);
126 703 bneuman
127 663 bneuman
        }
128
        else{
129
            i2c_putpacket(0x01, data, 2);
130
        }
131 80 bneuman
      }
132
    }
133
134 663 bneuman
    count = 0;
135
136 703 bneuman
    PORTB &= ~_BV(LEDG);
137
    PORTB |= _BV(LEDR);
138
139
140
    for(unsigned int i=0;i<60000;i++)
141 80 bneuman
    {
142
      delay_ms(1);
143 663 bneuman
      if(! (PINB & _BV(DETECT_12V)))
144 80 bneuman
      {
145 703 bneuman
          PORTB |= _BV(LEDR);
146 80 bneuman
          data[1]=I2C_MSG_CONTACT_ERROR;
147
          i2c_putpacket(0x01, data, 2);
148
149 703 bneuman
          if((++count)>=200)
150 80 bneuman
            break;
151
      }
152 663 bneuman
      else
153
        count = 0;
154 80 bneuman
155
    }
156
157
    data[1]=I2C_MSG_BATTERY_FULL;
158
    i2c_putpacket(0x01, data, 2);
159
  }
160
161 703 bneuman
}