Revision 692
Moved needed files into the trunk already.
branches/slam/code/behaviors/smart_run_around_fsm/smart_run_around_fsm.c | ||
---|---|---|
1 |
#include "dragonfly_lib.h" |
|
2 |
#include "smart_run_around_fsm.h" |
|
3 |
|
|
4 |
/*A simple behavior for navigating in an environment, i.e. avoiding walls and getting stuck. |
|
5 |
Could be better at not getting stuck. |
|
6 |
|
|
7 |
Latest revision only has two accessible states: move and reverse. |
|
8 |
*/ |
|
9 |
|
|
10 |
|
|
11 |
void run_around_init(void) |
|
12 |
{ |
|
13 |
range_init(); |
|
14 |
analog_init(); |
|
15 |
motors_init(); |
|
16 |
orb_init(); |
|
17 |
orb_enable(); |
|
18 |
usb_init(); |
|
19 |
|
|
20 |
/*Start in the default state, MOVING*/ |
|
21 |
avoid_state=MOVING; |
|
22 |
/*Set timers to their maximum values.*/ |
|
23 |
crazy_count=CRAZY_MAX; |
|
24 |
backup_count=0; |
|
25 |
pControl=0; |
|
26 |
|
|
27 |
/*Initialize distances to zero.*/ |
|
28 |
d1=0; d2=0; d3=0; d4=0; d5=0; |
|
29 |
|
|
30 |
orb_set_color(GREEN); |
|
31 |
|
|
32 |
} |
|
33 |
|
|
34 |
/*The main function, call this to update states as frequently as possible.*/ |
|
35 |
void run_around_FSM(void) { |
|
36 |
/*Default to moving.*/ |
|
37 |
avoid_state=MOVING; |
|
38 |
|
|
39 |
/*The following lines ensure that undefined (-1) values |
|
40 |
will not update the distances.*/ |
|
41 |
int temp; |
|
42 |
|
|
43 |
temp=range_read_distance(IR1); |
|
44 |
d1=(temp == -1) ? d1 : temp; |
|
45 |
|
|
46 |
temp=range_read_distance(IR2); |
|
47 |
d2=(temp == -1) ? d2 : temp; |
|
48 |
|
|
49 |
temp=range_read_distance(IR3); |
|
50 |
d3=(temp == -1) ? d3 : temp; |
|
51 |
|
|
52 |
temp=range_read_distance(IR4); |
|
53 |
d4=(temp == -1) ? d4 : temp; |
|
54 |
|
|
55 |
temp=range_read_distance(IR5); |
|
56 |
d5=(temp == -1) ? d5 : temp; |
|
57 |
|
|
58 |
/*If the crazy count is in it's >>3 range, it acts crazy.*/ |
|
59 |
if(crazy_count<=(CRAZY_MAX>>3)) |
|
60 |
{ |
|
61 |
avoid_state=CRAZY; |
|
62 |
crazy_count--; |
|
63 |
|
|
64 |
if(crazy_count<0) crazy_count=CRAZY_MAX; |
|
65 |
|
|
66 |
evaluate_state(); |
|
67 |
return; |
|
68 |
} |
|
69 |
|
|
70 |
//Checks the forward distance to see if it should back up, if so...state backwards. |
|
71 |
if((d2!=-1)&&(d2 >150)){ |
|
72 |
backup_count=BACKUP_MAX; |
|
73 |
avoid_state=BACKWARDS; |
|
74 |
evaluate_state(); |
|
75 |
return; |
|
76 |
} |
|
77 |
|
|
78 |
if(backup_count<BACKUP_MAX){ |
|
79 |
avoid_state=BACKWARDS; |
|
80 |
if(backup_count<0) |
|
81 |
backup_count=BACKUP_MAX; |
|
82 |
evaluate_state(); |
|
83 |
return; |
|
84 |
} |
|
85 |
|
|
86 |
/*Should evaluate an expression from -255 to 255 to pass to move.*/ |
|
87 |
pControl= ((d3-d1) + (d4-d5)) >> TURN_CONSTANT; |
|
88 |
|
|
89 |
if(pControl>PCONTROL_CRAZY_LIMIT || pControl<-PCONTROL_CRAZY_LIMIT) crazy_count--; |
|
90 |
/*i.e. if you really want to turn for an extended period of time...you're probably stuck.*/ |
|
91 |
|
|
92 |
/*Debug stuff:*/ |
|
93 |
/*usb_puts("pControl evaluating: "); |
|
94 |
usb_puti(pControl); |
|
95 |
usb_puts("\n\r"); |
|
96 |
usb_puts("IR1: "); |
|
97 |
usb_puti(d1); |
|
98 |
usb_puts(" IR2: "); |
|
99 |
usb_puti(d2); |
|
100 |
usb_puts(" IR3: "); |
|
101 |
usb_puti(d3); |
|
102 |
usb_puts(" IR4: "); |
|
103 |
usb_puti(d4); |
|
104 |
usb_puts(" IR5: "); |
|
105 |
usb_puti(d5); |
|
106 |
usb_puts("\n\r");*/ |
|
107 |
|
|
108 |
evaluate_state(); |
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
//Acts on state change. |
|
113 |
void evaluate_state(){ |
|
114 |
switch(avoid_state){ |
|
115 |
case(MOVING): orb_set_color(GREEN); |
|
116 |
move(STRAIT_SPEED,pControl); |
|
117 |
break; |
|
118 |
|
|
119 |
case(BACKWARDS): orb_set_color(ORANGE); |
|
120 |
move(-STRAIT_SPEED,0); |
|
121 |
break; |
|
122 |
|
|
123 |
case(CRAZY): orb_set_color(RED); |
|
124 |
/*TODO: Implement a crazy state.*/ |
|
125 |
move(STRAIT_SPEED,pControl); |
|
126 |
break; |
|
127 |
|
|
128 |
default: |
|
129 |
/*Should never get here, go strait.*/ |
|
130 |
move(100,0); orb_set_color(BLUE); |
|
131 |
break; |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
|
branches/slam/code/behaviors/smart_run_around_fsm/smart_run_around_fsm.h | ||
---|---|---|
1 |
//Obstacle Avoid Numbers |
|
2 |
|
|
3 |
|
|
4 |
#ifndef _RUN_AROUND_FSM_H_ |
|
5 |
#define _RUN_AROUND_FSM_H_ |
|
6 |
|
|
7 |
//The States: |
|
8 |
#define MOVING 12 //Move strait. |
|
9 |
#define BACKWARDS 15 //Move backwards. (Front close to wall.) |
|
10 |
#define STOP 16 //Stop. The default state, (Something broke). |
|
11 |
#define CRAZY 40 //Erratic behavior that occurs more often when the robot is frequently trying to turn. (i.e. may be stuck.) |
|
12 |
|
|
13 |
#define LEFT 37 //Left |
|
14 |
#define RIGHT 39 //Right |
|
15 |
|
|
16 |
#define BACKUP_MAX 15 |
|
17 |
#define CRAZY_MAX 200 //The number of counts between "crazy moments" |
|
18 |
#define STRAIT_SPEED 200 //The speed when going strait or backing up. |
|
19 |
#define TURN_CONSTANT 2 |
|
20 |
#define PCONTROL_CRAZY_LIMIT 80 |
|
21 |
|
|
22 |
int avoid_state; /*State machine variable.*/ |
|
23 |
int crazy_count; /*Counter for a 'get unstuck' behavior.*/ |
|
24 |
|
|
25 |
int backup_count; /*Counter for backup duration.*/ |
|
26 |
int pControl; /*Proportional control variable, determines turn direction.*/ |
|
27 |
int d1,d2,d3,d4,d5; /*The five distances taken in by IR.*/ |
|
28 |
|
|
29 |
void run_around_init(void); |
|
30 |
void run_around_FSM(void); |
|
31 |
void evaluate_state(void); |
|
32 |
|
|
33 |
#endif |
branches/slam/code/lib | ||
---|---|---|
1 |
link ../../../trunk/code/lib/ |
|
2 | 0 |
branches/slam/code/projects/autonomous_recharging/archs/USI_TWI_Master.h | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
/***************************************************************************** |
|
5 |
* |
|
6 |
* Atmel Corporation |
|
7 |
* |
|
8 |
* File : USI_TWI_Master.h |
|
9 |
* Compiler : IAR EWAAVR 2.28a/3.10a |
|
10 |
* Revision : $Revision: 1.11 $ |
|
11 |
* Date : $Date: Tuesday, September 13, 2005 09:09:36 UTC $ |
|
12 |
* Updated by : $Author: jtyssoe $ |
|
13 |
* |
|
14 |
* Support mail : avr@atmel.com |
|
15 |
* |
|
16 |
* Supported devices : All device with USI module can be used. |
|
17 |
* The example is written for the ATmega169, ATtiny26 and ATtiny2313 |
|
18 |
* |
|
19 |
* AppNote : AVR310 - Using the USI module as a TWI Master |
|
20 |
* |
|
21 |
* Description : This is an implementation of an TWI master using |
|
22 |
* the USI module as basis. The implementation assumes the AVR to |
|
23 |
* be the only TWI master in the system and can therefore not be |
|
24 |
* used in a multi-master system. |
|
25 |
* Usage : Initialize the USI module by calling the USI_TWI_Master_Initialise() |
|
26 |
* function. Hence messages/data are transceived on the bus using |
|
27 |
* the USI_TWI_Start_Transceiver_With_Data() function. If the transceiver |
|
28 |
* returns with a fail, then use USI_TWI_Get_Status_Info to evaluate the |
|
29 |
* couse of the failure. |
|
30 |
* |
|
31 |
****************************************************************************/ |
|
32 |
|
|
33 |
//********** Defines **********// |
|
34 |
|
|
35 |
// Defines controlling timing limits |
|
36 |
//#define TWI_FAST_MODE |
|
37 |
|
|
38 |
//#define SYS_CLK 4000.0 // [kHz] |
|
39 |
|
|
40 |
//Note, this is only a variable that determines how long to delay between |
|
41 |
// individual bits of I2C send. This has been tested down to 1MHz and it works. |
|
42 |
// It is possible that even slower speeds can be obtained. |
|
43 |
#define SYS_CLK 1.0 // [mHz] |
|
44 |
|
|
45 |
// TWI FAST mode timing limits. SCL = 100-400kHz |
|
46 |
//#define T2_TWI ((SYS_CLK *1300) /1000000) +1 // >1,3us |
|
47 |
//#define T4_TWI ((SYS_CLK * 600) /1000000) +1 // >0,6us |
|
48 |
|
|
49 |
|
|
50 |
//This may or may not make it faster. Uncomfirmed. But it works. |
|
51 |
#define T2_TWI 0.000001 |
|
52 |
#define T4_TWI 0.000001 |
|
53 |
|
|
54 |
// TWI STANDARD mode timing limits. SCL <= 100kHz |
|
55 |
//#define T2_TWI ((SYS_CLK *4700) /1000000) +1 // >4,7us |
|
56 |
//#define T4_TWI ((SYS_CLK *4000) /1000000) +1 // >4,0us |
|
57 |
|
|
58 |
//USI_TWI messages and flags and bit masks |
|
59 |
//#define SUCCESS 7 |
|
60 |
//#define MSG 0 |
|
61 |
/**************************************************************************** |
|
62 |
Bit and byte definitions |
|
63 |
****************************************************************************/ |
|
64 |
#define TWI_READ_BIT 0 // Bit position for R/W bit in "address byte". |
|
65 |
#define TWI_ADR_BITS 1 // Bit position for LSB of the slave address bits in the init byte. |
|
66 |
#define TWI_NACK_BIT 0 // Bit position for (N)ACK bit. |
|
67 |
|
|
68 |
#define USI_TWI_NO_DATA 0x00 // Transmission buffer is empty |
|
69 |
#define USI_TWI_DATA_OUT_OF_BOUND 0x01 // Transmission buffer is outside SRAM space |
|
70 |
#define USI_TWI_UE_START_CON 0x02 // Unexpected Start Condition |
|
71 |
#define USI_TWI_UE_STOP_CON 0x03 // Unexpected Stop Condition |
|
72 |
#define USI_TWI_UE_DATA_COL 0x04 // Unexpected Data Collision (arbitration) |
|
73 |
#define USI_TWI_NO_ACK_ON_DATA 0x05 // The slave did not acknowledge all data |
|
74 |
#define USI_TWI_NO_ACK_ON_ADDRESS 0x06 // The slave did not acknowledge the address |
|
75 |
#define USI_TWI_MISSING_START_CON 0x07 // Generated Start Condition not detected on bus |
|
76 |
#define USI_TWI_MISSING_STOP_CON 0x08 // Generated Stop Condition not detected on bus |
|
77 |
|
|
78 |
// Device dependant defines |
|
79 |
#define DDR_USI DDRB |
|
80 |
#define PORT_USI PORTB |
|
81 |
#define PIN_USI PINB |
|
82 |
#define PORT_USI_SDA PORTB0 |
|
83 |
#define PORT_USI_SCL PORTB2 |
|
84 |
#define PIN_USI_SDA PINB0 |
|
85 |
#define PIN_USI_SCL PINB2 |
|
86 |
|
|
87 |
// General defines |
|
88 |
#define TRUE 1 |
|
89 |
#define FALSE 0 |
|
90 |
|
|
91 |
//********** Prototypes **********// |
|
92 |
|
|
93 |
void USI_TWI_Master_Initialise( void ); |
|
94 |
unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char ); |
|
95 |
unsigned char USI_TWI_Get_State_Info( void ); |
branches/slam/code/projects/autonomous_recharging/archs/homing stop/Makefile | ||
---|---|---|
1 |
# Hey Emacs, this is a -*- makefile -*- |
|
2 |
#---------------------------------------------------------------------------- |
|
3 |
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al. |
|
4 |
# |
|
5 |
# Released to the Public Domain |
|
6 |
# |
|
7 |
# Additional material for this makefile was written by: |
|
8 |
# Peter Fleury |
|
9 |
# Tim Henigan |
|
10 |
# Colin O'Flynn |
|
11 |
# Reiner Patommel |
|
12 |
# Markus Pfaff |
|
13 |
# Sander Pool |
|
14 |
# Frederik Rouleau |
|
15 |
# |
|
16 |
#---------------------------------------------------------------------------- |
|
17 |
# On command line: |
|
18 |
# |
|
19 |
# make all = Make software. |
|
20 |
# |
|
21 |
# make clean = Clean out built project files. |
|
22 |
# |
|
23 |
# make coff = Convert ELF to AVR COFF. |
|
24 |
# |
|
25 |
# make extcoff = Convert ELF to AVR Extended COFF. |
|
26 |
# |
|
27 |
# make program = Download the hex file to the device, using avrdude. |
|
28 |
# Please customize the avrdude settings below first! |
|
29 |
# |
|
30 |
# make debug = Start either simulavr or avarice as specified for debugging, |
|
31 |
# with avr-gdb or avr-insight as the front end for debugging. |
|
32 |
# |
|
33 |
# make filename.s = Just compile filename.c into the assembler code only. |
|
34 |
# |
|
35 |
# make filename.i = Create a preprocessed source file for use in submitting |
|
36 |
# bug reports to the GCC project. |
|
37 |
# |
|
38 |
# To rebuild project do "make clean" then "make all". |
|
39 |
#---------------------------------------------------------------------------- |
|
40 |
|
|
41 |
|
|
42 |
# MCU name |
|
43 |
MCU = attiny861 |
|
44 |
|
|
45 |
|
|
46 |
# Processor frequency. |
|
47 |
# This will define a symbol, F_CPU, in all source code files equal to the |
|
48 |
# processor frequency. You can then use this symbol in your source code to |
|
49 |
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done |
|
50 |
# automatically to create a 32-bit value in your source code. |
|
51 |
F_CPU = 8000000 |
|
52 |
|
|
53 |
|
|
54 |
# Output format. (can be srec, ihex, binary) |
|
55 |
FORMAT = ihex |
|
56 |
|
|
57 |
|
|
58 |
# Target file name (without extension). |
|
59 |
TARGET = stop |
|
60 |
|
|
61 |
|
|
62 |
# List C source files here. (C dependencies are automatically generated.) |
|
63 |
SRC = $(TARGET).c |
|
64 |
|
|
65 |
|
|
66 |
# List Assembler source files here. |
|
67 |
# Make them always end in a capital .S. Files ending in a lowercase .s |
|
68 |
# will not be considered source files but generated files (assembler |
|
69 |
# output from the compiler), and will be deleted upon "make clean"! |
|
70 |
# Even though the DOS/Win* filesystem matches both .s and .S the same, |
|
71 |
# it will preserve the spelling of the filenames, and gcc itself does |
|
72 |
# care about how the name is spelled on its command-line. |
|
73 |
ASRC = |
|
74 |
|
|
75 |
|
|
76 |
# Optimization level, can be [0, 1, 2, 3, s]. |
|
77 |
# 0 = turn off optimization. s = optimize for size. |
|
78 |
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) |
|
79 |
OPT = s |
|
80 |
|
|
81 |
|
|
82 |
# Debugging format. |
|
83 |
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. |
|
84 |
# AVR Studio 4.10 requires dwarf-2. |
|
85 |
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. |
|
86 |
DEBUG = dwarf-2 |
|
87 |
|
|
88 |
|
|
89 |
# List any extra directories to look for include files here. |
|
90 |
# Each directory must be seperated by a space. |
|
91 |
# Use forward slashes for directory separators. |
|
92 |
# For a directory that has spaces, enclose it in quotes. |
|
93 |
#EXTRAINCDIRS = C:\WinAVR\include\fwr |
|
94 |
|
|
95 |
|
|
96 |
# Compiler flag to set the C Standard level. |
|
97 |
# c89 = "ANSI" C |
|
98 |
# gnu89 = c89 plus GCC extensions |
|
99 |
# c99 = ISO C99 standard (not yet fully implemented) |
|
100 |
# gnu99 = c99 plus GCC extensions |
|
101 |
CSTANDARD = -std=gnu99 |
|
102 |
|
|
103 |
|
|
104 |
# Place -D or -U options here |
|
105 |
CDEFS = -DF_CPU=$(F_CPU)UL |
|
106 |
|
|
107 |
|
|
108 |
# Place -I options here |
|
109 |
CINCS = |
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
#---------------- Compiler Options ---------------- |
|
114 |
# -g*: generate debugging information |
|
115 |
# -O*: optimization level |
|
116 |
# -f...: tuning, see GCC manual and avr-libc documentation |
|
117 |
# -Wall...: warning level |
|
118 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
119 |
# -adhlns...: create assembler listing |
|
120 |
CFLAGS = -g$(DEBUG) |
|
121 |
CFLAGS += $(CDEFS) $(CINCS) |
|
122 |
CFLAGS += -O$(OPT) |
|
123 |
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums |
|
124 |
CFLAGS += -Wall -Wstrict-prototypes |
|
125 |
CFLAGS += -Wa,-adhlns=$(<:.c=.lst) |
|
126 |
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) |
|
127 |
CFLAGS += $(CSTANDARD) |
|
128 |
|
|
129 |
|
|
130 |
#---------------- Assembler Options ---------------- |
|
131 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
132 |
# -ahlms: create listing |
|
133 |
# -gstabs: have the assembler create line number information; note that |
|
134 |
# for use in COFF files, additional information about filenames |
|
135 |
# and function names needs to be present in the assembler source |
|
136 |
# files -- see avr-libc docs [FIXME: not yet described there] |
|
137 |
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs |
|
138 |
|
|
139 |
|
|
140 |
#---------------- Library Options ---------------- |
|
141 |
# Minimalistic printf version |
|
142 |
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min |
|
143 |
|
|
144 |
# Floating point printf version (requires MATH_LIB = -lm below) |
|
145 |
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt |
|
146 |
|
|
147 |
# If this is left blank, then it will use the Standard printf version. |
|
148 |
PRINTF_LIB = |
|
149 |
#PRINTF_LIB = $(PRINTF_LIB_MIN) |
|
150 |
#PRINTF_LIB = $(PRINTF_LIB_FLOAT) |
|
151 |
|
|
152 |
|
|
153 |
# Minimalistic scanf version |
|
154 |
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min |
|
155 |
|
|
156 |
# Floating point + %[ scanf version (requires MATH_LIB = -lm below) |
|
157 |
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt |
|
158 |
|
|
159 |
# If this is left blank, then it will use the Standard scanf version. |
|
160 |
SCANF_LIB = |
|
161 |
#SCANF_LIB = $(SCANF_LIB_MIN) |
|
162 |
#SCANF_LIB = $(SCANF_LIB_FLOAT) |
|
163 |
|
|
164 |
|
|
165 |
MATH_LIB = -lm |
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
#---------------- External Memory Options ---------------- |
|
170 |
|
|
171 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
172 |
# used for variables (.data/.bss) and heap (malloc()). |
|
173 |
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff |
|
174 |
|
|
175 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
176 |
# only used for heap (malloc()). |
|
177 |
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff |
|
178 |
|
|
179 |
EXTMEMOPTS = |
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
#---------------- Linker Options ---------------- |
|
184 |
# -Wl,...: tell GCC to pass this to linker. |
|
185 |
# -Map: create map file |
|
186 |
# --cref: add cross reference to map file |
|
187 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref |
|
188 |
LDFLAGS += $(EXTMEMOPTS) |
|
189 |
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) |
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
#---------------- Programming Options (avrdude) ---------------- |
|
194 |
|
|
195 |
# Programming hardware: alf avr910 avrisp bascom bsd |
|
196 |
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500 |
|
197 |
# |
|
198 |
# Type: avrdude -c ? |
|
199 |
# to get a full listing. |
|
200 |
# |
|
201 |
AVRDUDE_PROGRAMMER = avrisp |
|
202 |
|
|
203 |
# com1 = serial port. Use lpt1 to connect to parallel port. |
|
204 |
AVRDUDE_PORT = com1 |
|
205 |
# programmer connected to serial device |
|
206 |
|
|
207 |
AVRDUDE_WRITE_FLASH = -b 4800 -U flash:w:$(TARGET).hex |
|
208 |
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep |
|
209 |
|
|
210 |
|
|
211 |
# Uncomment the following if you want avrdude's erase cycle counter. |
|
212 |
# Note that this counter needs to be initialized first using -Yn, |
|
213 |
# see avrdude manual. |
|
214 |
#AVRDUDE_ERASE_COUNTER = -y |
|
215 |
|
|
216 |
# Uncomment the following if you do /not/ wish a verification to be |
|
217 |
# performed after programming the device. |
|
218 |
#AVRDUDE_NO_VERIFY = -V |
|
219 |
|
|
220 |
# Increase verbosity level. Please use this when submitting bug |
|
221 |
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> |
|
222 |
# to submit bug reports. |
|
223 |
#AVRDUDE_VERBOSE = -v -v |
|
224 |
|
|
225 |
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) |
|
226 |
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) |
|
227 |
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) |
|
228 |
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) |
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
#---------------- Debugging Options ---------------- |
|
233 |
|
|
234 |
# For simulavr only - target MCU frequency. |
|
235 |
DEBUG_MFREQ = $(F_CPU) |
|
236 |
|
|
237 |
# Set the DEBUG_UI to either gdb or insight. |
|
238 |
# DEBUG_UI = gdb |
|
239 |
DEBUG_UI = insight |
|
240 |
|
|
241 |
# Set the debugging back-end to either avarice, simulavr. |
|
242 |
DEBUG_BACKEND = avarice |
|
243 |
#DEBUG_BACKEND = simulavr |
|
244 |
|
|
245 |
# GDB Init Filename. |
|
246 |
GDBINIT_FILE = __avr_gdbinit |
|
247 |
|
|
248 |
# When using avarice settings for the JTAG |
|
249 |
JTAG_DEV = /dev/com1 |
|
250 |
|
|
251 |
# Debugging port used to communicate between GDB / avarice / simulavr. |
|
252 |
DEBUG_PORT = 4242 |
|
253 |
|
|
254 |
# Debugging host used to communicate between GDB / avarice / simulavr, normally |
|
255 |
# just set to localhost unless doing some sort of crazy debugging when |
|
256 |
# avarice is running on a different computer. |
|
257 |
DEBUG_HOST = localhost |
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
#============================================================================ |
|
262 |
|
|
263 |
|
|
264 |
# Define programs and commands. |
|
265 |
SHELL = sh |
|
266 |
CC = avr-gcc |
|
267 |
OBJCOPY = avr-objcopy |
|
268 |
OBJDUMP = avr-objdump |
|
269 |
SIZE = avr-size |
|
270 |
NM = avr-nm |
|
271 |
AVRDUDE = avrdude |
|
272 |
REMOVE = rm -f |
|
273 |
COPY = cp |
|
274 |
WINSHELL = cmd |
|
275 |
|
|
276 |
|
|
277 |
# Define Messages |
|
278 |
# English |
|
279 |
MSG_ERRORS_NONE = Errors: none |
|
280 |
MSG_BEGIN = -------- begin -------- |
|
281 |
MSG_END = -------- end -------- |
|
282 |
MSG_SIZE_BEFORE = Size before: |
|
283 |
MSG_SIZE_AFTER = Size after: |
|
284 |
MSG_COFF = Converting to AVR COFF: |
|
285 |
MSG_EXTENDED_COFF = Converting to AVR Extended COFF: |
|
286 |
MSG_FLASH = Creating load file for Flash: |
|
287 |
MSG_EEPROM = Creating load file for EEPROM: |
|
288 |
MSG_EXTENDED_LISTING = Creating Extended Listing: |
|
289 |
MSG_SYMBOL_TABLE = Creating Symbol Table: |
|
290 |
MSG_LINKING = Linking: |
|
291 |
MSG_COMPILING = Compiling: |
|
292 |
MSG_ASSEMBLING = Assembling: |
|
293 |
MSG_CLEANING = Cleaning project: |
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
# Define all object files. |
|
299 |
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) |
|
300 |
|
|
301 |
# Define all listing files. |
|
302 |
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) |
|
303 |
|
|
304 |
|
|
305 |
# Compiler flags to generate dependency files. |
|
306 |
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d |
|
307 |
|
|
308 |
|
|
309 |
# Combine all necessary flags and optional flags. |
|
310 |
# Add target processor to flags. |
|
311 |
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) |
|
312 |
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) |
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
# Default target. |
|
319 |
all: begin gccversion sizebefore build sizeafter end |
|
320 |
|
|
321 |
build: elf hex eep lss sym |
|
322 |
|
|
323 |
elf: $(TARGET).elf |
|
324 |
hex: $(TARGET).hex |
|
325 |
eep: $(TARGET).eep |
|
326 |
lss: $(TARGET).lss |
|
327 |
sym: $(TARGET).sym |
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
# Eye candy. |
|
332 |
# AVR Studio 3.x does not check make's exit code but relies on |
|
333 |
# the following magic strings to be generated by the compile job. |
|
334 |
begin: |
|
335 |
@echo |
|
336 |
@echo $(MSG_BEGIN) |
|
337 |
|
|
338 |
end: |
|
339 |
@echo $(MSG_END) |
|
340 |
@echo |
|
341 |
|
|
342 |
|
|
343 |
# Display size of file. |
|
344 |
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex |
|
345 |
ELFSIZE = $(SIZE) -A $(TARGET).elf |
|
346 |
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU) |
|
347 |
|
|
348 |
sizebefore: |
|
349 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \ |
|
350 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
351 |
|
|
352 |
sizeafter: |
|
353 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \ |
|
354 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
# Display compiler version information. |
|
359 |
gccversion : |
|
360 |
@$(CC) --version |
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
# Program the device. |
|
365 |
program: $(TARGET).hex $(TARGET).eep |
|
366 |
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) |
|
367 |
|
|
368 |
|
|
369 |
# Generate avr-gdb config/init file which does the following: |
|
370 |
# define the reset signal, load the target file, connect to target, and set |
|
371 |
# a breakpoint at main(). |
|
372 |
gdb-config: |
|
373 |
@$(REMOVE) $(GDBINIT_FILE) |
|
374 |
@echo define reset >> $(GDBINIT_FILE) |
|
375 |
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE) |
|
376 |
@echo end >> $(GDBINIT_FILE) |
|
377 |
@echo file $(TARGET).elf >> $(GDBINIT_FILE) |
|
378 |
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE) |
|
379 |
ifeq ($(DEBUG_BACKEND),simulavr) |
|
380 |
@echo load >> $(GDBINIT_FILE) |
|
381 |
endif |
|
382 |
@echo break main >> $(GDBINIT_FILE) |
|
383 |
|
|
384 |
debug: gdb-config $(TARGET).elf |
|
385 |
ifeq ($(DEBUG_BACKEND), avarice) |
|
386 |
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays. |
|
387 |
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \ |
|
388 |
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT) |
|
389 |
@$(WINSHELL) /c pause |
|
390 |
|
|
391 |
else |
|
392 |
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \ |
|
393 |
$(DEBUG_MFREQ) --port $(DEBUG_PORT) |
|
394 |
endif |
|
395 |
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE) |
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
|
400 |
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. |
|
401 |
COFFCONVERT=$(OBJCOPY) --debugging \ |
|
402 |
--change-section-address .data-0x800000 \ |
|
403 |
--change-section-address .bss-0x800000 \ |
|
404 |
--change-section-address .noinit-0x800000 \ |
|
405 |
--change-section-address .eeprom-0x810000 |
|
406 |
|
|
407 |
|
|
408 |
coff: $(TARGET).elf |
|
409 |
@echo |
|
410 |
@echo $(MSG_COFF) $(TARGET).cof |
|
411 |
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof |
|
412 |
|
|
413 |
|
|
414 |
extcoff: $(TARGET).elf |
|
415 |
@echo |
|
416 |
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof |
|
417 |
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof |
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
# Create final output files (.hex, .eep) from ELF output file. |
|
422 |
%.hex: %.elf |
|
423 |
@echo |
|
424 |
@echo $(MSG_FLASH) $@ |
|
425 |
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ |
|
426 |
|
|
427 |
%.eep: %.elf |
|
428 |
@echo |
|
429 |
@echo $(MSG_EEPROM) $@ |
|
430 |
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ |
|
431 |
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@ |
|
432 |
|
|
433 |
# Create extended listing file from ELF output file. |
|
434 |
%.lss: %.elf |
|
435 |
@echo |
|
436 |
@echo $(MSG_EXTENDED_LISTING) $@ |
|
437 |
$(OBJDUMP) -h -S $< > $@ |
|
438 |
|
|
439 |
# Create a symbol table from ELF output file. |
|
440 |
%.sym: %.elf |
|
441 |
@echo |
|
442 |
@echo $(MSG_SYMBOL_TABLE) $@ |
|
443 |
$(NM) -n $< > $@ |
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
# Link: create ELF output file from object files. |
|
448 |
.SECONDARY : $(TARGET).elf |
|
449 |
.PRECIOUS : $(OBJ) |
|
450 |
%.elf: $(OBJ) |
|
451 |
@echo |
|
452 |
@echo $(MSG_LINKING) $@ |
|
453 |
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) |
|
454 |
|
|
455 |
|
|
456 |
# Compile: create object files from C source files. |
|
457 |
%.o : %.c |
|
458 |
@echo |
|
459 |
@echo $(MSG_COMPILING) $< |
|
460 |
$(CC) -c $(ALL_CFLAGS) $< -o $@ |
|
461 |
|
|
462 |
|
|
463 |
# Compile: create assembler files from C source files. |
|
464 |
%.s : %.c |
|
465 |
$(CC) -S $(ALL_CFLAGS) $< -o $@ |
|
466 |
|
|
467 |
|
|
468 |
# Assemble: create object files from assembler source files. |
|
469 |
%.o : %.S |
|
470 |
@echo |
|
471 |
@echo $(MSG_ASSEMBLING) $< |
|
472 |
$(CC) -c $(ALL_ASFLAGS) $< -o $@ |
|
473 |
|
|
474 |
# Create preprocessed source for use in sending a bug report. |
|
475 |
%.i : %.c |
|
476 |
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ |
|
477 |
|
|
478 |
|
|
479 |
# Target: clean project. |
|
480 |
clean: begin clean_list end |
|
481 |
|
|
482 |
clean_list : |
|
483 |
@echo |
|
484 |
@echo $(MSG_CLEANING) |
|
485 |
$(REMOVE) $(TARGET).hex |
|
486 |
$(REMOVE) $(TARGET).eep |
|
487 |
$(REMOVE) $(TARGET).cof |
|
488 |
$(REMOVE) $(TARGET).elf |
|
489 |
$(REMOVE) $(TARGET).map |
|
490 |
$(REMOVE) $(TARGET).sym |
|
491 |
$(REMOVE) $(TARGET).lss |
|
492 |
$(REMOVE) $(OBJ) |
|
493 |
$(REMOVE) $(LST) |
|
494 |
$(REMOVE) $(SRC:.c=.s) |
|
495 |
$(REMOVE) $(SRC:.c=.d) |
|
496 |
$(REMOVE) .dep/* |
|
497 |
|
|
498 |
|
|
499 |
|
|
500 |
# Include the dependency files. |
|
501 |
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) |
|
502 |
|
|
503 |
|
|
504 |
# Listing of phony targets. |
|
505 |
.PHONY : all begin finish end sizebefore sizeafter gccversion \ |
|
506 |
build elf hex eep lss sym coff extcoff \ |
|
507 |
clean clean_list program debug gdb-config |
|
508 |
|
branches/slam/code/projects/autonomous_recharging/archs/homing stop/stop.c | ||
---|---|---|
1 |
#include<avr/io.h> |
|
2 |
#include "i2c.h" |
|
3 |
|
|
4 |
#define LED1 PB4 //Green |
|
5 |
#define LED2 PB5 //Red |
|
6 |
|
|
7 |
int main( void ) |
|
8 |
{ |
|
9 |
DDRB = _BV(PB0) | _BV(PB5) | _BV(PB4); |
|
10 |
i2c_init(); |
|
11 |
|
|
12 |
/*tempData[0] = 'C'; |
|
13 |
tempData[1] = abs_time>>8; |
|
14 |
tempData[2] = abs_time&0xFF; |
|
15 |
i2c_putpacket(0x01, tempData, 3);*/ |
|
16 |
|
|
17 |
char data[2]; |
|
18 |
data[0]=I2C_MSG_DATA; |
|
19 |
|
|
20 |
while(1) |
|
21 |
{ |
|
22 |
PORTB=_BV(LED2); |
|
23 |
while(1) |
|
24 |
{ |
|
25 |
if(PINB & _BV(PB6)) |
|
26 |
{ |
|
27 |
PORTB = _BV(LED1); |
|
28 |
//This is a junk byte. For some reason the first packet is always ignored???? |
|
29 |
data[1] = 'a'; |
|
30 |
i2c_putpacket(0x01, data, 2); |
|
31 |
data[1]=I2C_MSG_BATTERY_CHARGING; |
|
32 |
i2c_putpacket(0x01, data, 2); |
|
33 |
break; |
|
34 |
} |
|
35 |
else |
|
36 |
{ |
|
37 |
data[1]=I2C_MSG_NO_CONTACT; |
|
38 |
i2c_putpacket(0x01, data, 2); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
for(int i=0;i<5000;i++) |
|
43 |
{ |
|
44 |
delay_ms(1); |
|
45 |
if(! PINB & _BV(PB6)) |
|
46 |
{ |
|
47 |
PORTB = _BV(LED2); |
|
48 |
data[1]=I2C_MSG_CONTACT_ERROR; |
|
49 |
i2c_putpacket(0x01, data, 2); |
|
50 |
|
|
51 |
while(! PINB & _BV(PB6)); |
|
52 |
break; |
|
53 |
} |
|
54 |
|
|
55 |
} |
|
56 |
|
|
57 |
data[1]=I2C_MSG_BATTERY_FULL; |
|
58 |
i2c_putpacket(0x01, data, 2); |
|
59 |
} |
|
60 |
|
|
61 |
} |
branches/slam/code/projects/autonomous_recharging/archs/ConstantCharging.c | ||
---|---|---|
1 |
#include <avr/io.h> |
|
2 |
#include <avr/interrupt.h> |
|
3 |
#include <avr/sleep.h> |
|
4 |
#include "i2c.h" |
|
5 |
#include "ring_buffer.h" |
|
6 |
|
|
7 |
|
|
8 |
// 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 |
|
|
22 |
#define SW0 PA6 |
|
23 |
#define HOMING_PIN PA7 |
|
24 |
|
|
25 |
#define DEBUG 0 |
|
26 |
|
|
27 |
#define MAX_T 300 |
|
28 |
#define MIN_T 730 |
|
29 |
//range is 0 to 45 C |
|
30 |
//cal tests: |
|
31 |
//room temp - 25 |
|
32 |
//value ~500, varies from battery to battery, but is consistent on one battery |
|
33 |
//freezer 737 |
|
34 |
//heat gun at a distance 461 |
|
35 |
|
|
36 |
#define MAX_DT -4 //this is the LOWEST ACCEPTABLE ADC value |
|
37 |
#define MAX_DT_ABS 400 |
|
38 |
#define VOLT_PLATEAU 50 |
|
39 |
|
|
40 |
|
|
41 |
//The following times are in seconds |
|
42 |
#define MAX_FAST_TIME 5400 |
|
43 |
#define MAX_TRICKLE_TIME 600 |
|
44 |
|
|
45 |
//debug pins |
|
46 |
#define debug_time PA3 |
|
47 |
#define debug_curr PA4 |
|
48 |
#define debug_volt PA5 |
|
49 |
#define debug_temp PA6 |
|
50 |
#define debug_12in PA7 |
|
51 |
|
|
52 |
//be sure admux also sets the MUX5 bit which is in ADCSRB |
|
53 |
#define ADMUX_I |
|
54 |
#define ADMUX_V |
|
55 |
#define ADMUX_T |
|
56 |
|
|
57 |
#define ROBOT_TX PB1 |
|
58 |
#define ROBOT_RX PB2 |
|
59 |
#define PWM PB3 |
|
60 |
#define DETECT_12V PB6 |
|
61 |
|
|
62 |
#define LED1 PB4 //Green |
|
63 |
#define LED2 PB5 //Red |
|
64 |
|
|
65 |
|
|
66 |
//LED States: |
|
67 |
//Red - Fast Charging |
|
68 |
//Green - Trickle Charging |
|
69 |
//Both steady - done charging |
|
70 |
//Both Blinking - Error |
|
71 |
|
|
72 |
#define INT_COUNT 2 //interrupts per second |
|
73 |
#define AVG_COUNT 64 //number of times to count current |
|
74 |
|
|
75 |
//To enable the PWM write : TCCR1B = (_Bv(CS10));//enable PWM |
|
76 |
|
|
77 |
uint8_t interrupt_count = INT_COUNT; |
|
78 |
|
|
79 |
volatile uint32_t abs_time=1; // start at one second so it doesnt do the minute checks right away |
|
80 |
volatile uint8_t new_second=0; //only used as a boolean |
|
81 |
|
|
82 |
volatile uint8_t error=0; |
|
83 |
volatile uint8_t status; |
|
84 |
|
|
85 |
volatile uint8_t steady_current = 0; |
|
86 |
|
|
87 |
//DT must be triggered twice in a row |
|
88 |
volatile uint8_t last_DT = 0; |
|
89 |
//same for DV |
|
90 |
volatile uint8_t last_DV = 0; |
|
91 |
|
|
92 |
#define FAST_CHARGE 1 |
|
93 |
#define TRICKLE_CHARGE 2 |
|
94 |
|
|
95 |
RING_BUFFER_NEW(ring_buffer, 12, int, buffer); |
|
96 |
|
|
97 |
void wait(int ops) |
|
98 |
{ |
|
99 |
int i = 0; |
|
100 |
while(i<ops) |
|
101 |
i++; |
|
102 |
} |
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
int avg_ADC(void) |
|
107 |
{ |
|
108 |
int av; |
|
109 |
char i; |
|
110 |
|
|
111 |
//Calculate a average out of the next 8 A/D conversions |
|
112 |
for(av=0,i=8;i;--i) |
|
113 |
{ |
|
114 |
ADCSRA |= _BV(ADSC); // start new A/D conversion |
|
115 |
while (!(ADCSRA & (_BV(ADIF)))) // wait until ADC is ready |
|
116 |
; |
|
117 |
av = av+ADC; |
|
118 |
} |
|
119 |
av = av/8; |
|
120 |
|
|
121 |
//ADCSRA &= ~_BV(ADEN); |
|
122 |
|
|
123 |
return av; |
|
124 |
|
|
125 |
} |
|
126 |
|
|
127 |
int get_voltage(void) |
|
128 |
{ |
|
129 |
ADMUX = _BV(MUX0); |
|
130 |
|
|
131 |
ADCSRB &= ~_BV(MUX5); |
|
132 |
|
|
133 |
return avg_ADC(); |
|
134 |
} |
|
135 |
|
|
136 |
int get_current(void) |
|
137 |
{ |
|
138 |
ADMUX = _BV(MUX1); |
|
139 |
|
|
140 |
ADCSRB |= _BV(MUX5); |
|
141 |
|
|
142 |
return avg_ADC(); |
|
143 |
} |
|
144 |
|
|
145 |
int get_temperature(void) |
|
146 |
{ |
|
147 |
ADMUX = _BV(MUX1); |
|
148 |
|
|
149 |
ADCSRB &= ~_BV(MUX5); |
|
150 |
|
|
151 |
return avg_ADC(); |
|
152 |
} |
|
153 |
|
|
154 |
int get_avg_voltage(void) |
|
155 |
{ |
|
156 |
int count=0; |
|
157 |
uint32_t sum=0; |
|
158 |
|
|
159 |
//OCR1B =120; |
|
160 |
while(count < AVG_COUNT) |
|
161 |
{ |
|
162 |
sum += get_voltage(); |
|
163 |
count++; |
|
164 |
} |
|
165 |
|
|
166 |
return sum/AVG_COUNT; |
|
167 |
} |
|
168 |
|
|
169 |
int get_avg_current(void) |
|
170 |
{ |
|
171 |
int count=0; |
|
172 |
uint32_t sum=0; |
|
173 |
|
|
174 |
//OCR1B =120; |
|
175 |
while(count < AVG_COUNT) |
|
176 |
{ |
|
177 |
sum += get_current(); |
|
178 |
count++; |
|
179 |
} |
|
180 |
|
|
181 |
return sum/AVG_COUNT; |
|
182 |
} |
|
183 |
|
|
184 |
int get_avg_temperature(void) |
|
185 |
{ |
|
186 |
int count=0; |
|
187 |
uint32_t sum=0; |
|
188 |
|
|
189 |
//OCR1B =120; |
|
190 |
while(count < AVG_COUNT) |
|
191 |
{ |
|
192 |
sum += get_temperature(); |
|
193 |
count++; |
|
194 |
} |
|
195 |
|
|
196 |
return sum/AVG_COUNT; |
|
197 |
} |
|
198 |
|
|
199 |
|
|
200 |
uint8_t supply_voltage(void) |
|
201 |
{ |
|
202 |
return PINB & _BV(DETECT_12V); |
|
203 |
} |
|
204 |
|
|
205 |
void clear_err(void) |
|
206 |
{ |
|
207 |
error=0; |
|
208 |
PORTB &= ~(_BV(LED1)|_BV(LED2)); |
|
209 |
|
|
210 |
if(status==FAST_CHARGE) |
|
211 |
PORTB |= _BV(LED2); |
|
212 |
|
|
213 |
if(status==TRICKLE_CHARGE) |
|
214 |
PORTB |= _BV(LED1); |
|
215 |
} |
|
216 |
|
|
217 |
void wait_8th(void) |
|
218 |
{ |
|
219 |
uint8_t start = abs_time % 8; |
|
220 |
|
|
221 |
while(abs_time % 8 == start) |
|
222 |
{ |
|
223 |
/*if(supply_voltage()) |
|
224 |
PORTB |= _BV(LED1); |
|
225 |
else |
|
226 |
PORTB &= ~_BV(LED1); |
|
227 |
if(get_voltage()>100) |
|
228 |
PORTB |= _BV(LED2); |
|
229 |
else |
|
230 |
PORTB &= ~_BV(LED2);*/ |
|
231 |
} |
|
232 |
} |
|
233 |
|
|
234 |
void send_err(void) |
|
235 |
{ |
|
236 |
OCR1B=0;//turn off the PWM to be safe |
|
237 |
|
|
238 |
PORTB &= ~(_BV(LED1)|_BV(LED2)); |
|
239 |
if(status!=0)//leave last error if there was one |
|
240 |
PORTA &= ~(_BV(debug_time)|_BV(debug_curr)|_BV(debug_volt)|_BV(debug_temp)|_BV(debug_12in)); |
|
241 |
error=1; |
|
242 |
status=0; |
|
243 |
} |
|
244 |
|
|
245 |
void send_done(void) |
|
246 |
{ |
|
247 |
char tempData; |
|
248 |
//Finished, leave |
|
249 |
tempData = 'F'; |
|
250 |
i2c_putpacket(0x01, &tempData, 1); |
|
251 |
|
|
252 |
PORTA &= ~(_BV(debug_time)|_BV(debug_curr)|_BV(debug_volt)|_BV(debug_temp)|_BV(debug_12in)); |
|
253 |
|
|
254 |
} |
|
255 |
|
|
256 |
void setup(void) |
|
257 |
{ |
|
258 |
DDRA = _BV(PA3); |
|
259 |
#ifdef debug |
|
260 |
//DDRA = (_BV(debug_time)|_BV(debug_curr)|_BV(debug_volt)|_BV(debug_temp)|_BV(debug_12in)); |
|
261 |
#endif |
|
262 |
PORTA = 0x00; |
|
263 |
DDRB = (_BV(ROBOT_TX)|_BV(PWM)|_BV(LED1)|_BV(LED2)); //confiure output pins |
|
264 |
PORTB = 0x00; |
|
265 |
|
|
266 |
ADCSRA = (_BV(ADEN)|_BV(ADPS2)|_BV(ADPS1)); //start ADC with a division factor of 64 |
|
267 |
|
|
268 |
TCCR0B = (_BV(CS01)); //set timer 0 for realtime mode |
|
269 |
TCCR0A = (_BV(TCW0)); |
|
270 |
TIMSK = (_BV(TOIE0)); //enable overflow interrupts |
|
271 |
|
|
272 |
TCCR1A = (_BV(COM1B1)|_BV(PWM1B)|_BV(COM1A1)|_BV(PWM1A)); //clear timer 1 on compare, set at 0x00. Fast PWM mode |
|
273 |
TCCR1B |= _BV(CS12)|_BV(CS10); //leave timer on and set compare to 0 to make output off |
|
274 |
OCR1B = 0; |
|
275 |
OCR1A = 0; |
|
276 |
|
|
277 |
|
|
278 |
RING_BUFFER_CLEAR(buffer); |
|
279 |
RING_BUFFER_INIT(buffer, 12); |
|
280 |
for(int i=0;i<10;i++) |
|
281 |
RING_BUFFER_ADD(buffer, 0); |
|
282 |
|
|
283 |
sei(); |
|
284 |
} |
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
//takes a 7-bit ionteger and displays it on the 7 LEDs with the Green being the MSB |
|
290 |
void LED_out(int i) |
|
291 |
{ |
|
292 |
if(i & 64) |
|
293 |
PORTB |= _BV(LED1); |
|
294 |
else |
|
295 |
PORTB &= ~_BV(LED1); |
|
296 |
|
|
297 |
if(i & 32) |
|
298 |
PORTB |= _BV(LED2); |
|
299 |
else |
|
300 |
PORTB &= ~_BV(LED2); |
|
301 |
|
|
302 |
if(i & 16) |
|
303 |
PORTA |= _BV(PA3); |
|
304 |
else |
|
305 |
PORTA &= ~_BV(PA3); |
|
306 |
|
|
307 |
if(i & 8) |
|
308 |
PORTA |= _BV(PA4); |
|
309 |
else |
|
310 |
PORTA &= ~_BV(PA4); |
|
311 |
|
|
312 |
if(i & 4) |
|
313 |
PORTA |= _BV(PA5); |
|
314 |
else |
|
315 |
PORTA &= ~_BV(PA5); |
|
316 |
|
|
317 |
if(i & 2) |
|
318 |
PORTA |= _BV(PA6); |
|
319 |
else |
|
320 |
PORTA &= ~_BV(PA6); |
|
321 |
|
|
322 |
if(i & 1) |
|
323 |
PORTA |= _BV(PA7); |
|
324 |
else |
|
325 |
PORTA &= ~_BV(debug_12in); |
|
326 |
} |
|
327 |
|
|
328 |
//get the difference of the current value minues the value 10 entires ago |
|
329 |
int ring_buffer_d10(int y) |
|
330 |
{ |
|
331 |
int x; |
|
332 |
RING_BUFFER_REMOVE(buffer, x); |
|
333 |
|
|
334 |
RING_BUFFER_ADD(buffer, y); |
|
335 |
return y-x; |
|
336 |
} |
|
337 |
|
|
338 |
|
|
339 |
uint8_t read_homing() |
|
340 |
{ |
|
341 |
uint8_t ret = PINA & _BV(HOMING_PIN); |
|
342 |
if(ret) |
|
343 |
PORTA |= _BV(PA3); |
|
344 |
else |
|
345 |
PORTA &= ~_BV(PA3); |
|
346 |
return ret; |
|
347 |
} |
|
348 |
|
|
349 |
//copied from scheduler/seeking.c |
|
350 |
uint8_t get_delay(void) |
|
351 |
{ |
|
352 |
uint8_t count = 0; |
|
353 |
|
|
354 |
PORTB|=_BV(LED2); |
|
355 |
while(read_homing()) |
|
356 |
{ |
|
357 |
delay_ms(1); |
|
358 |
count++; |
|
359 |
if (count >= 100) |
|
360 |
return 1; |
|
361 |
} //wait a beacon cycle to make sure we aren't starting the count in the middle of one |
|
362 |
PORTB&=~_BV(LED2); |
|
363 |
count = 0; |
|
364 |
PORTB|=_BV(LED1); |
|
365 |
while(!read_homing()) |
|
366 |
{ |
|
367 |
delay_ms(1); |
|
368 |
count++; |
|
369 |
if(count==255) |
|
370 |
return 2; |
|
371 |
} |
|
372 |
PORTB&=~_BV(LED1); |
|
373 |
|
|
374 |
/*RECH_PUTS("\n\rCount: "); |
|
375 |
RECH_PUTI(count); |
|
376 |
RECH_PUTC('.');*/ |
|
377 |
|
|
378 |
return count; |
|
379 |
} |
|
380 |
|
|
381 |
void trickle_charge(void) |
|
382 |
{ |
|
383 |
|
|
384 |
abs_time = 0; |
|
385 |
status = 0; |
|
386 |
char tempData[5]; |
|
387 |
char data[2]; |
|
388 |
data[0]='D'; |
|
389 |
int volt = 0; |
|
390 |
int temp = 0; |
|
391 |
int curr = 0; |
|
392 |
int meas_count = 0; |
|
393 |
int mod=0; |
|
394 |
sei(); |
|
395 |
OCR1B = 0; |
|
396 |
|
|
397 |
while(status!=2) |
|
398 |
{ |
|
399 |
mod=abs_time%4; |
|
400 |
|
|
401 |
if(supply_voltage()) |
|
402 |
while(abs_time%4==mod); |
|
403 |
|
|
404 |
/* TIME TERMINATION */ |
|
405 |
if(abs_time>500) //12000=25 minutes |
|
406 |
{ |
|
407 |
//SEND_DONE |
|
408 |
OCR1B=0; |
|
409 |
break; |
|
410 |
} |
|
411 |
|
|
412 |
#if DEBUG |
|
413 |
tempData[0] = 'C'; |
|
414 |
tempData[1] = abs_time>>8; |
|
415 |
tempData[2] = abs_time&0xFF; |
|
416 |
i2c_putpacket(0x01, tempData, 3); |
|
417 |
#endif |
|
418 |
|
|
419 |
mod=abs_time%4; |
|
420 |
while(abs_time%4==mod) |
|
421 |
{ |
|
422 |
/* CONTACT */ |
|
423 |
if(supply_voltage()) |
|
424 |
{ |
|
425 |
//curr = regulate_current(500); |
|
426 |
curr = get_avg_current(); |
|
427 |
|
|
428 |
if(status==0) |
|
429 |
{ |
|
430 |
status=1; |
|
431 |
data[1]='a'; |
|
432 |
i2c_putpacket(0x01, data, 2); |
|
433 |
data[1]=I2C_MSG_BATTERY_CHARGING; |
|
434 |
i2c_putpacket(0x01, data, 2); |
|
435 |
} |
|
436 |
|
|
437 |
/* Trickle Charge */ |
|
438 |
if(status==1) |
|
439 |
OCR1B = 50; |
|
440 |
} |
|
441 |
/* NO CONTACT */ |
|
442 |
else |
|
443 |
{ |
|
444 |
if(status==1) |
|
445 |
{ |
|
446 |
status=0; |
|
447 |
data[1]=I2C_MSG_CONTACT_ERROR; |
|
448 |
i2c_putpacket(0x01, data, 2); |
|
449 |
} |
|
450 |
else |
|
451 |
{ |
|
452 |
get_delay(); //reject the first reading |
|
453 |
data[0]=I2C_MSG_HOMING; |
|
454 |
data[1]=get_delay(); |
|
455 |
i2c_putpacket(0x01, data, 2); |
|
456 |
|
|
457 |
data[0]='D'; |
|
458 |
} |
|
459 |
curr = 0; |
|
460 |
OCR1B = 0; |
|
461 |
} |
|
462 |
} |
|
463 |
|
|
464 |
#if DEBUG |
|
465 |
tempData[0] = 'P'; |
|
466 |
tempData[1] = 0; |
|
467 |
tempData[2] = OCR1B; |
|
468 |
i2c_putpacket(0x01, tempData, 3); |
|
469 |
tempData[0] = 'I'; |
|
470 |
tempData[1] = curr>>8; |
|
471 |
tempData[2] = curr&0xFF; |
|
472 |
i2c_putpacket(0x01, tempData, 3); |
|
473 |
#endif |
|
474 |
curr=6666; |
|
475 |
|
|
476 |
/* Absolute Voltage Termination */ |
|
477 |
if(supply_voltage()) |
|
478 |
{ |
|
479 |
mod=abs_time%4; |
|
480 |
while(abs_time%4==mod) |
|
481 |
{ |
|
482 |
volt = get_avg_voltage(); |
|
483 |
} |
|
484 |
|
|
485 |
if(volt>1010) |
|
486 |
{ |
|
487 |
//SEND ERROR |
|
488 |
status=0; |
|
489 |
} |
|
490 |
|
|
491 |
#if DEBUG |
|
492 |
tempData[0] = 'V'; |
|
493 |
tempData[1] = volt>>8; |
|
494 |
tempData[2] = volt&0xFF; |
|
495 |
i2c_putpacket(0x01, tempData, 3); |
|
496 |
#endif |
|
497 |
volt=6666; |
|
498 |
|
|
499 |
/* Absolute Temperature Termination */ |
|
500 |
mod=abs_time%4; |
|
501 |
while(abs_time%4==mod) |
|
502 |
{ |
|
503 |
temp = get_avg_temperature(); |
|
504 |
} |
|
505 |
|
|
506 |
if(temp<250) |
|
507 |
{ |
|
508 |
//SEND ERROR |
|
509 |
status=0; |
|
510 |
} |
|
511 |
|
|
512 |
#if DEBUG |
|
513 |
tempData[0] = 'T'; |
|
514 |
tempData[1] = temp>>8; |
|
515 |
tempData[2] = temp&0xFF; |
|
516 |
i2c_putpacket(0x01, tempData, 3); |
|
517 |
#endif |
|
518 |
} |
|
519 |
|
|
520 |
temp=6666; |
|
521 |
} |
|
522 |
|
|
523 |
data[1]=I2C_MSG_BATTERY_FULL; |
|
524 |
i2c_putpacket(0x01, data, 2); |
|
525 |
|
|
526 |
} |
|
527 |
|
|
528 |
int main(void) |
|
529 |
{ |
|
530 |
new_second=0; |
|
531 |
char tempData[5]; //For i2c communication |
|
532 |
//test_board(); |
|
533 |
|
|
534 |
setup(); |
|
535 |
i2c_init(); |
|
536 |
|
|
537 |
|
|
538 |
/*GIMSK = (_BV(PCIE0)); //enable PCINT interrupts |
|
539 |
PCMSK1 = (_BV(PCINT10)); //enable pin change interrupt on ROBOT_RX |
|
540 |
MCUCR = (_BV(SE)|_BV(SM1));// (power-down mode) |
|
541 |
*/ |
|
542 |
|
|
543 |
OCR1B=0; |
|
544 |
|
|
545 |
sei(); |
|
546 |
|
|
547 |
//test delay_ms |
|
548 |
PORTB|=_BV(LED2); |
|
549 |
PORTB|=_BV(LED1); |
|
550 |
delay_ms(1000); |
|
551 |
PORTB&=~_BV(LED2); |
|
552 |
PORTB&=~_BV(LED1); |
|
553 |
|
|
554 |
//******************************* |
|
555 |
while(1) |
|
556 |
trickle_charge(); |
|
557 |
|
|
558 |
/*GIMSK = (_BV(PCIE0)); //enable PCINT interrupts |
|
559 |
sleep_cpu();*/ |
|
560 |
|
|
561 |
PORTB=0;//clear outputs |
|
562 |
|
|
563 |
GIMSK = 0; |
|
564 |
|
|
565 |
error=0; |
|
566 |
|
|
567 |
i2c_init(); |
|
568 |
int volt=0, last_volt=0, same_volt=0; |
|
569 |
int temp=0, dt; |
|
570 |
int curr=0; |
|
571 |
int meas_count; |
|
572 |
int mod=0; |
|
573 |
|
|
574 |
status=FAST_CHARGE; |
|
575 |
|
|
576 |
while(1) |
|
577 |
{ |
|
578 |
mod=abs_time%4; |
|
579 |
while(abs_time%4==mod); |
|
580 |
|
|
581 |
/*if((abs_time>>3)%3==0) |
|
582 |
OCR1B=21; |
|
583 |
else if((abs_time>>3)%3==1) |
|
584 |
OCR1B=57; |
|
585 |
else |
|
586 |
OCR1B=85;*/ |
|
587 |
|
|
588 |
tempData[0] = 'C'; |
|
589 |
tempData[1] = abs_time>>8; |
|
590 |
tempData[2] = abs_time&0xFF; |
|
591 |
i2c_putpacket(0x01, tempData, 3); |
|
592 |
|
|
593 |
mod=abs_time%4; |
|
594 |
while(abs_time%4==mod) |
|
595 |
{ |
|
596 |
/* CONTACT */ |
|
597 |
if(supply_voltage()) |
|
598 |
{ |
|
599 |
//curr = regulate_current(500); |
|
600 |
curr = get_avg_current(); |
|
601 |
|
|
602 |
if(status==FAST_CHARGE) |
|
603 |
OCR1B=50; |
|
604 |
} |
|
605 |
/* NO CONTACT */ |
|
606 |
else |
|
607 |
{ |
|
608 |
curr = 0; |
|
609 |
OCR1B = 0; |
|
610 |
} |
|
611 |
} |
|
612 |
|
|
613 |
|
|
614 |
tempData[0] = 'P'; |
|
615 |
tempData[1] = 0; |
|
616 |
tempData[2] = OCR1B; |
|
617 |
i2c_putpacket(0x01, tempData, 3); |
|
618 |
tempData[0] = 'I'; |
|
619 |
tempData[1] = curr>>8; |
|
620 |
tempData[2] = curr&0xFF; |
|
621 |
i2c_putpacket(0x01, tempData, 3); |
|
622 |
curr=6666; |
|
623 |
|
|
624 |
mod=abs_time%4; |
Also available in: Unified diff