/****************************************************************** hardware.h Define the Macros & hardware dependent stuff here. History: ======= - August 25th 2002 / Stephane Gauthier / Genesis - September 17th 2002 / Stephane Gauthier / Adapted code to better AVRGCC 3.2's assignment mode. - October 6th 2002 / Stephane Gauthier / Added ADC & GPD2D12 constants. - October 17th 2002 / Stephane Gauthier / Tweaked PID loop rate to 50Hz. - December 21st 2002 / Stephane Gauthier / Pointed PWM_L constant to OC2 instead of OC1A as the pin is dead. (change with new CPU) - January 12th 2003 / Stephane Gauthier / Ported back to STK-300 board with atmega128. Reverted PWM_L back to OC1A. - January 14th 2003 / Stephane Gauthier / Increased TICK_RATE to satisfy NYQUIST, when polling encoders. - January 27th 2003 / Stephane Gauthier / Added I2C stuff. - March 24th 2003 / Stephane Gauthier / Modified wheel base and diameter for new wheels. - March 28th 2003 / Stephane Gauthier / Added constants to support Line sensor calibration at startup. *******************************************************************/ /****************** PIN ASSIGNMENT TABLE ************************** Pin Equate I/O Direction Bits ---------------------------------------------------------- A7 OUTPUT 1 TRIS Word = 0xFF A6 OUTPUT 1 A5 OUTPUT 1 A4 OUTPUT 1 A3 OUTPUT 1 A2 OUTPUT 1 A1 OUTPUT 1 A0 OUTPUT 1 B7 HEARTBEAT_LED OUTPUT 1 TRIS Word = 0xFF B6 PWM_L OUTPUT 1 B5 PWM_R OUTPUT 1 B4 LED4 OUTPUT 1 B3 LED3 OUTPUT 1 B2 LED2 OUTPUT 1 B1 LED1 OUTPUT 1 B0 LED0 OUTPUT 1 C7 OUTPUT 1 TRIS Word = 0xFF C6 OUTPUT 1 C5 OUTPUT 1 C4 OUTPUT 1 C3 OUTPUT 1 C2 OUTPUT 1 C1 OUTPUT 1 C0 OUTPUT 1 D7 SCM_CALIBRATE_REQ INPUT 0 TRIS Word = 0x3C D6 LINE_CALIBRATE_REQ INPUT 0 D5 OUTPUT 1 D4 OUTPUT 1 D3 OUTPUT 1 D2 OUTPUT 1 D1 SDA INPUT 0 D0 SCL INPUT 0 E7 ENCODER_R_A INPUT 0 TRIS Word = 0x06 E6 ENCODER_R_B INPUT 0 E5 ENCODER_L_A INPUT 0 E4 ENCODER_L_B INPUT 0 E3 MOTION_SENSE INPUT 0 E2 FAN_CTL OUTPUT 1 E1 TXD0 OUTPUT 1 E0 RXD0 INPUT 0 F7 LINE_SENSOR_R INPUT 0 TRIS Word = 0x20 F6 LINE_SENSOR_L INPUT 0 F5 OUTPUT 1 F4 GPD_R_B INPUT 0 F3 GPD_R_F INPUT 0 F2 GPD_L_B INPUT 0 F1 GPD_L_F INPUT 0 F0 GPD_FRONT INPUT 0 ******************************************************************/ //#include // For AVR Port & Bits definitions used in hardware.h #include // General Constants #define AVR 1 #define AVR_ENHANCED 1 #define TRUE 1 #define FALSE 0 #define ON 1 #define OFF 0 #define RIGHT 1 #define LEFT 0 #define ABSOLUTE 1 // For rotate behaviour #define RELATIVE 0 // System Clock settings: CPU/256, 4khz tickrate #define CPUCLK 16000000L // CPU xtal #define TICKRATE 6000 // AvrX timer queue Tick rate (approx 8% of real-time) #define BAUDRATE 19200L // UART baudrate #define RESET() { void (*funcptr)(void) = 0x0000; asm ("cli\n"); funcptr(); } // To provide a software reset! #define UBRR_INIT (CPUCLK/(16 * BAUDRATE)-1) // UART config // Basic bit handling Macros to work around bug in GCC to permit the use of defines for both port and bit arguments. #define bit_set(x) sbi(x) #define bit_clear(x) cbi(x) #define bit_test(x) bit_is_set(x) #define bit_toggle(x) if (bit_is_set(x)) cbi(x); else sbi(x) #define loop_til_set(x) loop_until_bit_is_set(x) #define loop_til_clear(x) loop_until_bit_is_clear(x) // Other macros #define MAX(x , y) ((x) > (y) ? (x) : (y)) #define MIN(x , y) ((x) < (y) ? (x) : (y)) // Timing Macros #define MILLISECOND(A) ((A)*(long)TICKRATE/1000) #define SECOND(A) ((A)*(long)TICKRATE) // Limited to a maximum of 15 seconds depending on TICK_RATE used. #define HERTZ(A) (TICKRATE/(A)) // Peripheral initialization #define TCCR0_INIT (BV(CS2) | BV(CS1)) #define TIMER0_PRESCALE 256 // Keep in Sync with TCCR0_INIT!!! #define TCNT0_INIT (256-(CPUCLK/TIMER0_PRESCALE/TICKRATE)) #define TIMSK_INIT (BV(TOIE0)) #define MCUCR_INIT (BV(SE)) // ( | BV(ISC01) | BV(ISC00) | BV(ISC11) | BV(ISC10)) #define TCCR1A_INIT ((BV(WGM10) | BV(COM1A1) | BV(COM1B1))) // Enable dual 8-bit PWM #define TCCR1B_INIT (BV(CS0)) // prescale of 1:1 to get PWM speed of ~30Khz w/ 16Mhz crystal. #define EIMSK_INIT (BV(INT5) | BV(INT7)) // Enable External Ints 4&7 (Left and Right encoders) #define EICRA_INIT (BV(ISC41) | BV(ISC40) | BV(ISC71) | BV(ISC70)) #define ADCSR_INIT (BV(ADEN) | BV(ADPS2) | BV(ADPS0)) // | BV(ADSC) | BV(ADIE) | BV(ADFR)) // 32:1 ADC prescale #define ADMUX_INIT (BV(ADLAR)) // use STK-300's external adjustable voltage reference. #define SFIOR_INIT (BV(ADHSM)) // Enable ADC high-speed mode. (more power cons.) // Port direction initialization constants (see I/O port usage map above) #define DDRA_INIT 0xFF // Port A #define DDRB_INIT 0xFF // Port B #define DDRC_INIT 0xFF // Port C #define DDRD_INIT 0x3C // Port D #define DDRE_INIT 0x06 // Port E #define DDRF_INIT 0x20 // Port F // Port initial value. (or pull-ups setup if bit is an input) (see I/O usage map above) #define PORTA_INIT 0x00 #define PORTB_INIT 0x00 #define PORTC_INIT 0x00 #define PORTD_INIT 0x00 #define PORTE_INIT 0x00 #define PORTF_INIT 0x00 // Task Priorities #define ARBITRATE_PRIORITY 2 // Arbitrate trask #define EXEC_PRIORITY 4 // Exec task #define MOTOR_PRIORITY 1 // Motor task #define AVOID_PRIORITY 3 // Avoid task #define FOLLOW_PRIORITY 5 // Follow task #define GOAT_PRIORITY 5 // Goto task #define FORWARD_PRIORITY 5 // Forward task #define ROTATE_PRIORITY 5 // Rotate task #define EXTINGUISH_PRIORITY 5 // Extinguish task #define SEARCH_PRIORITY 5 // Rotate task #define PLANNER_PRIORITY 4 // Planner task #define ALIGN_PRIORITY 5 // Align // Behaviour IDs #define EXEC 0 #define FOLLOW 1 #define ROTATE 2 #define FORWARD 3 #define GOAT 4 #define AVOID 5 #define ROAM 6 #define EXTINGUISH 7 #define SEARCH 8 #define ALIGN 9 #define BEHAVIOUR_COUNT 10 // Keep this updated! // Behaviour priority constants #define BEHAVIOUR_PRIORITY_MAX 8+1 // max: 0 - 8 // Behaviour Loop Delay constants #define FOLLOW_LOOP_DELAY MILLISECOND(35) // Wall Following loop delay #define AVOID_LOOP_DELAY MILLISECOND(40) // Avoid loop delay #define GOAT_LOOP_DELAY MILLISECOND(30) // GoAt loop delay #define ALIGN_LOOP_DELAY MILLISECOND(35) // Align loop delay #define PID_LOOP_DELAY HERTZ(PID_RATE) // PID loop (motor speed constants assume this remains static!) // Misc. Constants #define STARTUP_DELAY SECOND(2.5) // Time to wait before starting bot. // LED Debug Constants #define LED0 PORTB, 0 #define LED1 PORTB, 1 #define LED2 PORTB, 2 #define LED3 PORTB, 3 #define LED4 PORTB, 4 // Debug #define HEARTBEAT_LED PORTB, 7 // Heartbeat Debug LED #define EXEC_KEY '`' // Character to enter EXEC #define SCM_CALIBRATE_REQ PIND, 7 // Switch to calibrate SCM #define LINE_CALIBRATE_REQ PIND, 6 // Switch to calibrate Line sensors // Motor control constants #define PWM_R OCR1AL // For 9 or 10 bits PWM #define PWM_L OCR1BL // For 9 or 10 bits PWM #define MAX_PWM_OUTPUT 0xFF/2 // Update for different PWM resolutions. #define POWER(X) (int)(X*MAX_TICK_RATE/100) // TO convert from % of power to PID ticks. #define K_O 10 // Output scale factor to permit use of fraction in K constants #define K_P 2.7*K_O #define K_I 0//0.7*K_O #define K_D 3.3*K_O #define K_ACCEL (int)0x0100 // Means 01.00 (hex) #define PID_RATE (unsigned char)50 // Hz // motor hardware constants #define GEAR_RATIO (unsigned char)150 #define MOTOR_SPEED_MAX (unsigned char)106 //Rev per second (6400 RPM @ 24V) #define ENCODER_SEGMENTS (unsigned char)10 #define TICKS_PER_MOTOR_REV (unsigned char)(ENCODER_SEGMENTS*2) // full quadrature decoding. (X2) #define TICKS_PER_SECOND_MAX (unsigned int)(MOTOR_SPEED_MAX*TICKS_PER_MOTOR_REV) #define TICKS_PER_WHEEL_REV (unsigned int)(TICKS_PER_MOTOR_REV*GEAR_RATIO) #define MAX_TICK_RATE (unsigned char)((TICKS_PER_SECOND_MAX/PID_RATE)-1) #define WHEEL_BASE (float)25.4 // in centimeters #define WHEEL_DIAMETER (float)16.1 // in centimeters #define WHEEL_CIRC (float)(M_PI*WHEEL_DIAMETER) #define DIST_PER_TICK (float)(WHEEL_CIRC/TICKS_PER_WHEEL_REV) // centimeters per tick #define TICK_PER_DIST (float)(1/DIST_PER_TICK) #define DEG_PER_TICK (float)(DIST_PER_TICK/WHEEL_BASE) #define ONE_DEG (int)((2*M_PI)/(DEG_PER_TICK*360)/2.0) // Ticks/degree (normalized: no <<8 built-in) //#define ONE_DEG (int)((2*M_PI*256)/(DEG_PER_TICK*360)) //#define DISTANCE(A) (float)(M_PI*(A)/TICKS_PER_WHEEL_REV) #define RAD2DEG (float)(180.0/M_PI) // Optical encoders constants #define ENCODER_L_B _BV(PIN5) #define ENCODER_L_A _BV(PIN4) #define ENCODER_R_A _BV(PIN6) #define ENCODER_R_B _BV(PIN7) #define ENCODER_MASK 0xF0 // Mask of other bits. #define ENCODER_PORT PINE // ADC channels constants #define GPD_F 0 #define GPD_L_F 1 #define GPD_L_B 2 #define GPD_R_F 3 #define GPD_R_B 4 #define LINE_SENSOR_L 6 #define LINE_SENSOR_R 7 #define GPD_SAMPLES 2 // number of samples to average out per gpd read. // Wall Following Controler Constants (all distances are in centimeters) #define HALL_WIDTH 46 #define BASE_DIAMETER 27 #define FOLLOW_DISTANCE_SETPOINT (unsigned char)(((HALL_WIDTH - BASE_DIAMETER)/2)-1) #define D_INTER_SENSOR (float)21.5 // distance between the two side sensors in cm. #define FOLLOW_SIDE_THRESHOLD 25 // side threshold distance (cm) #define FOLLOW_FRONT_THRESHOLD 16 // front threshold distance (cm) (front sensor is not precise so distance means nothing) #define F_O 10 // wall following PD controller coefficients. #define F_P 2.0*F_O #define F_D 1.0*F_O #define FOLLOW_STEERING_FACTOR 2 // Steering coeffecient #define FOLLOW_DISTANCE_FACTOR 1 // Steering coeffecient #define FOLLOW_FORWARD_SPEED POWER(85) // Speed at which to travel forward #define FOLLOW_REPORT_FAILURE 0 // Error! #define FOLLOW_REPORT_SUCCESS 1 // End of wall report. #define FOLLOW_REPORT_OBSTACLE 2 // Front Obstacle report. #define FOLLOW_REPORT_DOOR 3 // Door frame detected report. // Avoid constants #define AVOID_FRONT_THRESHOLD 15 // Distance threshold (cm) at which AVOID reacts. (account for reaction time by making it a bit longer) #define AVOID_SIDE_THRESHOLD 3 #define AVOID_TURN_SPEED POWER(30) // Align constants #define ALIGN_STEERING_FACTOR 1 #define ALIGN_SIDE_THRESHOLD FOLLOW_SIDE_THRESHOLD #define ALIGN_RIGHT 1 // Align with Right wall #define ALIGN_LEFT 0 // Align with Left wall #define ALIGN_DOOR 2 // Align with door frame #define ALIGN_REPORT_SUCCESS 1 // Aligned successfully #define ALIGN_REPORT_FAILURE 0 // Failure #define ALIGN_CORRECTION_SPEED POWER(34) // Speed to move forward/backwards when wall missing? #define ALIGN_DOOR_SPEED_LO POWER(36) // Door alignment low speed #define ALIGN_CLEAR_DISTANCE 8 // Distance to move forward once on door step to clear it. #define A_O 10 // Alignment PD controller coefficients #define A_P 2.0*A_O #define A_D 1.0*A_O // Go_At constants #define GOAT_DISTANCE_THRESHOLD 3 // distance tolerance in cm. // Forward constant #define FORWARD_FRONT_THRESHOLD FOLLOW_FRONT_THRESHOLD #define FORWARD_SPEED POWER(85) // Speed at which we travel #define FORWARD_REPORT_FAILURE 0 // Moved specified distance unsuccessfully. #define FORWARD_REPORT_SUCCESS 1 // Moved specified distance successfully. #define FORWARD_REPORT_OBSTACLE 2 // Front Obstacle report. #define FORWARD_REPORT_DOOR 3 // Door frame detected report. #define FORWARD_CLEAR_HOME_CIRCLE 8 // Distance to move forward on startup to clear home circle. // Extinguish constants #define EXTINGUISH_SAMPLE_DELAY MILLISECOND(5) #define EXTINGUISH_APPROACH_COUNT 13 // Number of cycles to keep approaching candle in white circle #define EXTINGUISH_SCAN_SPEED POWER(28) // Speed at which to rotate when scanning for candle #define EXTINGUISH_FORWARD_SPEED POWER(40) // Speed to move forward towards candle #define EXTINGUISH_THRESHOLD_ERROR 1 // Error signal below which we are considered "locked on" #define EXTINGUISH_HUNT_ANGLE 7 // How many degrees to turn when unable to extinguish candle. #define E_O 10 // fractional Divisor. #define E_P 2 // Proportional coefficient // Fan control #define FAN_CTL PORTE, 2 #define FAN_DELAY SECOND(4) // How long to leave fan on in seconds. // Motion Sensor input #define MOTION_SENSE PINE, 3 // Line sensors constants #define LINE_SENSOR_THRESHOLD 0x8f // Default threshold #define LINE_SENSOR(x) (unsigned char)(x > R_ParameterTable.line_threshold) // Official trinity config: '<' #define LINE_SENSOR_LEFT 1 // To report back which sensor triggered. #define LINE_SENSOR_RIGHT 2 #define LINE_SENSOR_BOTH 3 #define LINE_CALIBRATE_FUDGE_FACTOR (unsigned char)0.02*LINE_CALIBRATE_FUDGE_DIV //How much margin of error to use #define LINE_CALIBRATE_FUDGE_DIV 100 // Fixed decimal divisor #define LINE_CALIBRATE_SAMPLES 4 // Number of samples to calibrate with. // I2C Constants #define TWBR_INIT 72 // TWI interface frequency divisor for 100Khz (See Data Sheet for details) #define TWPS_INIT 0 // TWI prescaler of 1 (see data sheet) #define TWCR_INIT _BV(TWEN) // Enable TWI module // Search constants #define SEARCH_ENTER_DISTANCE 16 //BASE_DIAMETER/2 // How far to move in a room before searching. // SCM init constants #define SCM_DEF_EXPOSURE_H 0x06 #define SCM_DEF_EXPOSURE_L 0x00 #define SCM_DEF_MIN_SIZE 4 #define SCM_DEF_THRESHOLD 230