Statistics
| Branch: | Revision:

root / paintboard / code / main.c @ 11c945d9

History | View | Annotate | Download (2.58 KB)

1 4319e0bc Tom Mullins
#include "twi.h"
2
#include "motor.h"
3 351481ed Tom Mullins
#include "servo.h"
4
#include "sol.h"
5 11c945d9 Julian Binder
#include "geiger.h"
6 4319e0bc Tom Mullins
#include <avr/io.h>
7
#include <avr/interrupt.h>
8 d9b50611 Tom Mullins
#include <util/delay.h>
9 4319e0bc Tom Mullins
10
#define TRACKING_ID 0x43
11
#define SERIAL_NUMBER 0x12
12
13
// indicies for paintboard internal data
14
#define PAINT_TRACKING_ID       0 // ro
15
#define PAINT_SERIAL_NUMBER     1 // ro
16
#define PAINT_MOTOR_A           2 // r/w
17
#define PAINT_MOTOR_B           3 // r/w
18
#define PAINT_SERVO_A           4 // r/w
19
#define PAINT_SERVO_B           5 // r/w
20
#define PAINT_12V_1             6 // r/w
21
#define PAINT_12V_2             7 // r/w
22
#define PAINT_12V_3             8 // r/w
23
#define PAINT_12V_4             9 // r/w
24 1af81271 Julian Binder
#define PAINT_INPUT_1           10 //ro
25
#define PAINT_INPUT_2           11 //ro
26
#define PAINT_INPUT_3           12 //ro
27 4319e0bc Tom Mullins
28 1af81271 Julian Binder
29
#define PAINT_DATA_LEN         13
30
31 d9b50611 Tom Mullins
#define METAL_DETECT           PD5
32 4319e0bc Tom Mullins
33
uint8_t internal_index = 0;
34
uint8_t internal_data[PAINT_DATA_LEN] = {
35
  TRACKING_ID,
36
  SERIAL_NUMBER,
37
  0,
38
  0,
39
  0,
40
  0,
41
  0,
42
  0,
43
  0,
44 c874b753 Tom Mullins
  0,
45
  0,
46
  0,
47 4319e0bc Tom Mullins
  0
48
};
49
50
void slave_rx(uint8_t* data, int len)
51
{
52
  uint8_t value;
53
  if (len > 0 && data[0] < PAINT_DATA_LEN)
54
  {
55
    internal_index = data[0];
56
    if (len > 1 && internal_index > 1)
57
    {
58
      value = data[1];
59
      internal_data[internal_index] = value;
60
      switch (internal_index) {
61
        case PAINT_MOTOR_A:
62 351481ed Tom Mullins
          set_motor1(value);
63 4319e0bc Tom Mullins
          break;
64
        case PAINT_MOTOR_B:
65 351481ed Tom Mullins
          set_motor2(value);
66 4319e0bc Tom Mullins
          break;
67
        case PAINT_SERVO_A:
68 351481ed Tom Mullins
          set_servo1(value);
69 4319e0bc Tom Mullins
          break;
70
        case PAINT_SERVO_B:
71 351481ed Tom Mullins
          set_servo2(value);
72 4319e0bc Tom Mullins
          break;
73
        case PAINT_12V_1:
74 351481ed Tom Mullins
          set_sol1(value);
75 4319e0bc Tom Mullins
          break;
76
        case PAINT_12V_2:
77 351481ed Tom Mullins
          set_sol2(value);
78 4319e0bc Tom Mullins
          break;
79
        case PAINT_12V_3:
80 351481ed Tom Mullins
          set_sol3(value);
81 4319e0bc Tom Mullins
          break;
82
        case PAINT_12V_4:
83 351481ed Tom Mullins
          set_sol4(value);
84 4319e0bc Tom Mullins
          break;
85
      }
86
    }
87
  }
88
}
89
90
void slave_tx(void)
91
{
92
  twi_transmit(&internal_data[internal_index], 1);
93
  internal_index++;
94
  if (internal_index >= PAINT_DATA_LEN)
95
    internal_index = 0;
96
}
97
98
int main()
99
{
100
  sei();
101
  twi_attachSlaveRxEvent(slave_rx);
102
  twi_attachSlaveTxEvent(slave_tx);
103
  twi_setAddress(TRACKING_ID);
104
  twi_init();
105 11c945d9 Julian Binder
  geiger_init();
106 4319e0bc Tom Mullins
  motor_init();
107 c874b753 Tom Mullins
  servo_init();
108 4319e0bc Tom Mullins
  while (1)
109
  {
110 11c945d9 Julian Binder
    geiger_tick();
111
    internal_data[PAINT_INPUT_2] = geiger_rate();
112 1af81271 Julian Binder
    internal_data[PAINT_INPUT_1] = !(!(_BV(METAL_DETECT) & PIND));
113 d9b50611 Tom Mullins
    if (internal_data[PAINT_INPUT_1]) {
114
      set_servo1(-128);
115 fcddafaf Julian Binder
      set_motor1(0);
116
      set_motor2(0);
117 d9b50611 Tom Mullins
    } else {
118
      set_servo1(127);
119 fcddafaf Julian Binder
      set_motor1(127);
120
      set_motor2(127);
121 d9b50611 Tom Mullins
    }
122
    servo_pulse();
123
    _delay_ms(20);
124 4319e0bc Tom Mullins
  }
125
  return 0;
126
}