Project

General

Profile

Revision cc9ca04e

IDcc9ca04e4d58158606627a644331754a3fd64e91
Parent 812788aa
Child 68b23184, fd73d758

Added by Thomas Mullins over 11 years ago

Fixed pins to work with 128rfa1 instead of 2560

View differences:

scout_avr/Makefile
1 1

  
2
#PART=m128rfa1
3
#MCU=atmega128rfa1
2
PART=m128rfa1
3
MCU=atmega128rfa1
4 4

  
5 5
#PART=m328p
6 6
#MCU=atmega328
7 7

  
8
PART=m2560
9
MCU=atmega2560
8
#PART=m2560
9
#MCU=atmega2560
10 10

  
11
#PROG=avrispMKII
12
PROG=stk600
11
PROG=avrispMKII
12
#PROG=stk600
13 13

  
14 14
F_CPU=16000000
15 15
SRC=src/*.cpp src/ros_lib/*.cpp
......
25 25
	avr-g++ $(FLAGS) $(SRC) -o scout_avr.elf
26 26

  
27 27
program: scout_avr.hex
28
	avrdude -p $(PART) -c $(PROG) -P usb -B 1 -F -U flash:w:scout_avr.hex
28
	avrdude -p $(PART) -c $(PROG) -P usb -B 5 -U flash:w:scout_avr.hex
29 29

  
30 30
clean:
31 31
	rm -f scout_avr.elf scout_avr.hex
scout_avr/src/bom.cpp
53 53

  
54 54
void bom_isr() {
55 55
  if (out_high) {
56
    BOM_EMIT ^= out_pin_mask;
56
    PORT_BOM_EMIT ^= out_pin_mask;
57 57
  }
58 58
}
59 59

  
......
66 66

  
67 67
static void start_38kHz_signal() {
68 68
  TCNT3 = 0;
69
  BOM_EMIT |= out_pin_mask;
69
  PORT_BOM_EMIT |= out_pin_mask;
70 70
  out_high = 1;
71 71
}
72 72

  
73 73
static void stop_38kHz_signal() {
74
  BOM_EMIT &= ~ out_pin_mask;
74
  PORT_BOM_EMIT &= ~ out_pin_mask;
75 75
  out_high = 0;
76 76
}
77 77

  
......
126 126
  PCICR |= _BV(PCIE0);
127 127
  
128 128
  // BOM_EMIT as output
129
  DDRH |= _BV(DDH4) | _BV(DDH5) | _BV(DDH6) | _BV(DDH7);
129
  DDRF |= _BV(DDF4) | _BV(DDF5) | _BV(DDF6) | _BV(DDF7);
130 130

  
131 131
  init_38kHz_signal();
132 132
  init_data_signal();
......
134 134

  
135 135
void bom_send(char dir) {
136 136
  switch (dir) {
137
    case BOM_FRONT: out_pin_mask = _BV(BOM_EMIT0); break;
138
    case BOM_LEFT:  out_pin_mask = _BV(BOM_EMIT1); break;
139
    case BOM_RIGHT: out_pin_mask = _BV(BOM_EMIT2); break;
140
    case BOM_BACK:  out_pin_mask = _BV(BOM_EMIT3); break;
137
    case BOM_FRONT: out_pin_mask = _BV(P_BOM_EMIT0); break;
138
    case BOM_LEFT:  out_pin_mask = _BV(P_BOM_EMIT1); break;
139
    case BOM_RIGHT: out_pin_mask = _BV(P_BOM_EMIT2); break;
140
    case BOM_BACK:  out_pin_mask = _BV(P_BOM_EMIT3); break;
141 141
  }
142 142
  out_counter = 16;
143 143
  out_msg = sharp_msg_make(0x2A, bom_msg_make(robot_id, dir));
......
188 188
}
189 189

  
190 190
ISR(PCINT0_vect) {
191
  uint8_t bom_sig = BOM_SIG;
191
  uint8_t bom_sig = PIN_BOM_SIG;
192 192
  uint8_t changed = bom_sig ^ prev_bom_sig;
193
  if (changed & _BV(BOM_SIG0)) recv_edge(bom_sig & _BV(BOM_SIG0), &bom_rx[0]);
194
  if (changed & _BV(BOM_SIG1)) recv_edge(bom_sig & _BV(BOM_SIG1), &bom_rx[1]);
195
  if (changed & _BV(BOM_SIG2)) recv_edge(bom_sig & _BV(BOM_SIG2), &bom_rx[2]);
196
  if (changed & _BV(BOM_SIG3)) recv_edge(bom_sig & _BV(BOM_SIG3), &bom_rx[3]);
193
  if (changed & _BV(P_BOM_SIG0)) recv_edge(bom_sig & _BV(P_BOM_SIG0), &bom_rx[0]);
194
  if (changed & _BV(P_BOM_SIG1)) recv_edge(bom_sig & _BV(P_BOM_SIG1), &bom_rx[1]);
195
  if (changed & _BV(P_BOM_SIG2)) recv_edge(bom_sig & _BV(P_BOM_SIG2), &bom_rx[2]);
196
  if (changed & _BV(P_BOM_SIG3)) recv_edge(bom_sig & _BV(P_BOM_SIG3), &bom_rx[3]);
197 197
  prev_bom_sig = bom_sig;
198 198
}
199 199

  
scout_avr/src/bom.h
22 22

  
23 23
// i/o pins
24 24
// if these are changed, remember to change bom_init and/or ISRs
25
#define BOM_SIG PINB
26
#define BOM_SIG0 PB0
27
#define BOM_SIG1 PB1
28
#define BOM_SIG2 PB2
29
#define BOM_SIG3 PB3
30
#define BOM_EMIT PORTH
31
#define BOM_EMIT0 PH4
32
#define BOM_EMIT1 PH5
33
#define BOM_EMIT2 PH6
34
#define BOM_EMIT3 PH7
25
#define PIN_BOM_SIG PINB
26
#define P_BOM_SIG0 PB0
27
#define P_BOM_SIG1 PB1
28
#define P_BOM_SIG2 PB2
29
#define P_BOM_SIG3 PB3
30

  
31
#define PORT_BOM_EMIT PORTF
32
#define P_BOM_EMIT0 PF4
33
#define P_BOM_EMIT1 PF5
34
#define P_BOM_EMIT2 PF6
35
#define P_BOM_EMIT3 PF7
35 36

  
36 37
typedef uint8_t bom_msg_t;
37 38

  
......
48 49
void set_robot_id(char id);
49 50
char get_robot_id(void);
50 51

  
51
// NOTE: call range_init before bom_init to ensure timer 1 is set up!
52
// NOTE: call range_init before bom_init to ensure timer 5 is set up!
52 53
void bom_init(void);
53 54
void bom_send(char dir);
54 55
int bom_get(char dir);
scout_avr/src/main.cpp
1
#if 0 ///////////////////////////////////////////////
1
#if 1 ///////////////////////////////////////////////
2 2

  
3 3
#include "ros.h"
4 4
#include <std_msgs/Int16.h>
scout_avr/src/range.cpp
1
#include "range.h"
2

  
3 1
extern "C"
4 2
{
5 3
#include <avr/io.h>
6 4
#include <avr/interrupt.h>
7 5
}
6
#include "range.h"
8 7

  
9 8
/* Ultrasonic Sensor:
10 9
 * -if RX pin is left open, it will continuously take readings
......
12 11
 * -PW will be high for a maximum of 37.5ms if no target is detected
13 12
 * 
14 13
 * 37.5ms * 8 MHz / 8 prescaler = 37500 max wait
14
 * 37.5ms * 16 MHz / 8 prescaler = problem
15 15
 */
16 16

  
17
// so that we can test on a 328 or the stk600
18
#if defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega2560__)
19
#  define read_INT0 (PIND & _BV(PD0))
20
#  define read_INT1 (PIND & _BV(PD1))
21
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
22
#  define read_INT0 (PIND & _BV(PD2))
23
#  define read_INT1 (PIND & _BV(PD3))
24
#else
25
#  error "Please define read_INTx for this device"
26
#endif
27

  
28 17
struct range_t {
29 18
  unsigned int start; // timer value on rising edge
30 19
  unsigned int value; // last measured range
......
33 22
static void on_edge(int which)
34 23
{
35 24
  unsigned char int_high;
36
  // TODO centralize timer 5
37
  // TODO ensure this is in microseconds even for 16MHz
38 25
  unsigned int time = TCNT5;
39 26
  
40 27
  if (which)
41 28
  {
42
    int_high = read_INT1;
29
    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM1);
43 30
  }
44 31
  else
45 32
  {
46
    int_high = read_INT0;
33
    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM0);
47 34
  }
48 35
  
49 36
  if (int_high)
......
57 44
  }
58 45
}
59 46

  
60
ISR(INT0_vect)
47
ISR(INT3_vect)
61 48
{
62 49
  on_edge(0);
63 50
}
64 51

  
65
ISR(INT1_vect)
52
ISR(INT2_vect)
66 53
{
67 54
  on_edge(1);
68 55
}
......
70 57
void range_init()
71 58
{
72 59
  // ISCx = 1, edge triggered
73
  EICRA |= _BV(ISC10) | _BV(ISC00);
74
  // enable INT0 and INT1
75
  EIMSK |= _BV(INT1) | _BV(INT0);
60
  EICRA |= _BV(ISC20) | _BV(ISC30);
61
  // enable INT2 and INT3
62
  EIMSK |= _BV(INT2) | _BV(INT3);
76 63
  
77 64
  // CS1 = 2, 1/8 prescaler
78 65
  // if this is changed, remember to change recv_edge in bom.cpp!
scout_avr/src/range.h
3 3

  
4 4
#define RANGE_ERR 0xFFFF
5 5

  
6
#define PIN_SONAR_PWM PIND
7
#define P_SONAR_PWM1 PD2
8
#define P_SONAR_PWM0 PD3
9

  
10
#define PORT_SONAR_TX PORTG
11
#define P_SONAR_TX PG1
12

  
6 13
void range_init();
7 14
unsigned int range_get(int which);
8 15

  

Also available in: Unified diff