Statistics
| Branch: | Revision:

root / forklift / code / forklift.c @ 86196a57

History | View | Annotate | Download (2.94 KB)

1
/*
2
 * ARM -> us
3
 * -height setpoint
4
 *
5
 * us -> ARM
6
 * -tracking ID (same for all forklifts)
7
 * -serial number (unique to each forklift)
8
 * -current height
9
 * -current height setpoint
10
 * -payload weight
11
 *
12
 */
13
 
14
#include "twi.h"
15
#include "analog.h"
16
#include "motor.h"
17
#include <avr/io.h>
18
#include <avr/interrupt.h>
19

    
20
#define TRACKING_ID 0x41
21
#define SERIAL_NUMBER 0x12
22

    
23
// indicies for forklift internal data
24
#define FORKLIFT_TRACKING_ID       0
25
#define FORKLIFT_SERIAL_NUMBER     1
26
#define FORKLIFT_HEIGHT            2
27
#define FORKLIFT_HEIGHT_SETPOINT   3 // r/w
28
#define FORKLIFT_LINE_POS          4
29
#define FORKLIFT_LINE_THRESH_HIGH  5 // r/w
30
#define FORKLIFT_LINE_THRESH_LOW   6 // r/w
31
#define FORKLIFT_LINE_VALS_START   7
32
#define FORKLIFT_LINE_VALS_END    12 // non-inclusive
33

    
34
#define FORKLIFT_DATA_LEN         12
35

    
36
uint8_t internal_index = 0;
37
uint8_t internal_data[] = {
38
  TRACKING_ID,
39
  SERIAL_NUMBER,
40
  0,
41
  100, // default height setpoint
42
  0,
43
  0,
44
  150, // default line threshold
45
  0,
46
  0,
47
  0,
48
  0,
49
  0
50
};
51

    
52
int error;
53
int i_term;
54

    
55
void init_int0(void)
56
{
57
  TCCR0B = (1<<CS02)|(1<<CS00); //Timer clock = system clock / 1024
58
  TIFR0 = 1<<TOV0; //Clear TOV0  clear pending interrupts
59
  TIMSK0 = 1<<TOIE0; //Enable Timer0 Overflow Interrupt
60
  
61
}
62
void slave_rx(uint8_t* data, int len)
63
{
64
  if (len > 0 && data[0] < FORKLIFT_DATA_LEN)
65
  {
66
    internal_index = data[0];
67
    if (len > 1)
68
  {
69
    if (internal_index == FORKLIFT_HEIGHT_SETPOINT) {
70
        internal_data[internal_index] = data[1];
71
        i_term = 0;
72
    }
73
    else if (internal_index == FORKLIFT_LINE_THRESH_HIGH)
74
    {
75
      internal_data[internal_index] = data[1];
76
    line_set_threshold_high(data[1]);
77
    }
78
      else if (internal_index == FORKLIFT_LINE_THRESH_LOW)
79
    {
80
      internal_data[internal_index] = data[1];
81
    line_set_threshold_low(data[1]);
82
    }
83
  }
84
  }
85
}
86

    
87
void slave_tx(void)
88
{
89
  twi_transmit(&internal_data[internal_index], 1);
90
  internal_index++;
91
  if (internal_index >= FORKLIFT_DATA_LEN)
92
    internal_index = 0;
93
}
94

    
95
int clamp(int min, int max, int val)
96
{
97
  if (val > max) return max;
98
  if (val < min) return min;
99
  return val;
100
}
101

    
102
SIGNAL(TIMER0_OVF_vect)
103
{
104
  // TODO make this more easily tunable (despite integer limitations)
105
  int height = (int)internal_data[FORKLIFT_HEIGHT];
106
  error = (int)internal_data[FORKLIFT_HEIGHT_SETPOINT] - height;
107
  i_term = i_term + error/4;
108
  int speed = clamp(-127, 127, i_term/12 + error*4);
109
  if (height < 15 && speed < 0) speed = 0;
110
  if (height > 240 && speed > 0) speed = 0;
111
  set_motor(speed);
112
}
113

    
114
int main()
115
{
116
  int i;
117
  error = 0;
118
  i_term = 0;
119
  init_int0();
120
  sei();
121
  twi_attachSlaveRxEvent(slave_rx);
122
  twi_attachSlaveTxEvent(slave_tx);
123
  twi_setAddress(TRACKING_ID);
124
  twi_init();
125
  analog_init();
126
  motor_init();
127
  while (1)
128
  {
129
    for (i = 0; i < 5; i++)
130
      internal_data[i + FORKLIFT_LINE_VALS_START] = line_read(i) >> 2;
131
    internal_data[FORKLIFT_LINE_POS] = line_read_pos();
132
    internal_data[FORKLIFT_HEIGHT] = 255 - (analog_read(ADC_HEIGHT) >> 2);
133
  }
134
  return 0;
135
}