vel_control.c 8.22 KB
Newer Older
1
2
3
4
5
6
#include <avr/twi.h>
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>

7
8
9
10
#include "pccom.h"
#include "vel_control.h"


11
// reference variables
12
13
volatile int32_t ref = 0;        // 11 frac bits
volatile int32_t refCtrl = 0;    // ref used in ctrl-loop (=ref sent from simulink)
14
15
16


// velocity control variables
17
18
19
20
21
22
23
24
25
26
volatile int32_t u = 0;          // 11 frac bits
volatile int32_t uSend = 0;      // ctrl sent to simulink
volatile int32_t v = 0;          // 11 frac bits
volatile int32_t vSat = 0;
volatile int8_t brake = 0;       // brake variable if pos-sample missed
volatile int32_t I = 0;          // 11 frac bits
volatile int32_t e = 0;          // 11 frac bits
volatile int8_t intCond = 0;


Pontus Giselsson's avatar
Pontus Giselsson committed
27
28
volatile int32_t K = 807;       // 6 frac bits, prop constant
volatile int32_t Ke = 13;        // 6 frac bits, integral constant
29
volatile int8_t fr_comp = (10<<3);
Pontus Giselsson's avatar
Pontus Giselsson committed
30
31
#define V_MAX (120<<4)
#define V_MIN (-120<<4)
32

33
34

// encoder variables
35
#define ENCODERY  (PIND&0x04)        //Positional encoder pins
36
37
38
39
40
41
42
43
44
45
46
#define ENCODERX  ((PINB&0x02)<<1)   //Positional encoder pins (shift one step for faster comparison with Y)

// position variables
volatile int16_t pos = 0;        // position in tics
volatile int16_t posTemp = 0;    // position sent to simulink
volatile int16_t posCtrl = 0;    // position used in ctrl-loop
volatile int16_t oldPos = 0;     // previous pos used for velocity estimation
volatile int8_t newX;            // encoder signal
volatile int8_t newY;            // encoder signal
volatile int8_t oldX;            // previous encoder signal
volatile int8_t oldY;            // previous encoder signal
47
48

// velocity estimation parameters
49
50
51
52
volatile int32_t velEst = 0;     // vel-estimate, 5 frac bits
volatile int32_t velEstSend = 0; // vel-estimate sent to simulink
int16_t a = 116;                 // 7 frac bits (parameter in velocity estimate)
int16_t b = 152;                 // 5 frac bits (parameter in velocity estimate)
53

54
// adc measurement variables
55
56
57
58
59
volatile int16_t low;            // temporary variable for ad-reading
volatile int16_t high;           // temporary variable for ad-reading
volatile int16_t angleOffset = 0;

/* return position (in tics) */
60
int32_t getPosition() {
61
  cli();
62
  posTemp = pos;
63
  sei();
64
  return ((int32_t) posTemp);
65
66
67
}


68
/* return velocity (in mm/s) */
69
int32_t getVelocity() {
70
71
72
  return velEstSend;
}

73
74

/* return last angle measurement */
75
76
77
int16_t getAngle() {
  low = inp(ADCL);
  high = inp(ADCH);
78
  return (((int16_t) ((high<<8) | low) - 512)-angleOffset);
79
80
}

81
82

/* return current-reference */
83
84
int32_t getCurrentRef() {
  return uSend;
85
86
}

87

88
/* Set new reference value */
89
90
91
92
void setRef(int32_t newRef) {
  ref = newRef;
}

93
94
95
96
97
98
99
/* Routine used to initialize the positional encoders */
void initPos()
{
  oldX = ENCODERX;
  oldY = ENCODERY;
}

100
/* Timer 2, Encoder counter, 73 kHz updates position variable */
101
SIGNAL(SIG_OUTPUT_COMPARE2) {
102

103
104
105
  // Update position from encoder
  newX = ENCODERX;
  newY = ENCODERY;
106
107
  if((newX != oldX) || (newY != oldY)) {                            //Check if any value changed
      /*
108
109
      sum = (oldX<<2)+oldY+newX+(newY>>2);  
      if (sum == 2 || sum == 4 || sum == 11 || sum == 13) {
110
	pos = pos+1;
111
      } else if (sum == 1 || sum == 7 || sum ==  8 || sum == 14) {
112
113
114
115
116
	pos = pos-1;
      } else {
	brake = 1; // emergency brake
      }
      */
117
    // Works like if-clauses above, but faster!
118
119
120
121
      if ((oldX == newY) && (oldY != newX)) {
	pos = pos+1;
      } else if ((oldX != newY) && (oldY == newX)) {
	pos = pos-1;
122
123
124
125
126
      } else {
	brake = 1;
      }
      oldX = newX;
      oldY = newY;
127
128
129
130
131
  }

}


132
/* Timer 0, serial communication with simulink */
133
134
SIGNAL(SIG_OUTPUT_COMPARE0) {

135
136
137
  TIMSK &= ~(BV(OCIE0)|BV(OCIE1A));   // Disable communication and ctrl-interrupts

  sei(); // enable interrupts from encoder-counter
Pontus Giselsson's avatar
Pontus Giselsson committed
138
139
  
  
140

141
  // Poll UART receiver
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  uint8_t status = UCSRA;
  if (status & (1<<RXC)) {
    char ch = UDR;
    pccom_receiveByte(ch);
    
    if (status & ((1<<FE)|(1<<DOR)|(1<<PE))) { 
      //main_emergencyStop(); // stop on USART error
    }     
  }
  
  // Poll UART sender
  if (UCSRA & (1<<UDRE)) {
    int16_t toSend = pccom_getNextByteToSend();
    if (toSend >= 0) UDR = (char)toSend;
  }
Pontus Giselsson's avatar
Pontus Giselsson committed
157

158
  
Pontus Giselsson's avatar
Pontus Giselsson committed
159

160
  TIFR = (1<<OCF0);        // skip pending interrupts from serial comm, (but not from ctrl)
161

162
  TIMSK |= (BV(OCIE0)|BV(OCIE1A)); // reenable communication and ctrl-interrupts
163

164
165
166
167
}



168
169
170
/* Timer 0, control loop , 1 kHz */
SIGNAL(SIG_OUTPUT_COMPARE1A) {
  
171
  posCtrl = pos; // store pos to use in this loop
172

173
  sei(); // to enable interupts from encoder counter and communication
174

175
  // velocity estimation in mm/s
176
177
178
  velEst = (((a*velEst+64)>>7)+b*(posCtrl-oldPos));  // 5 fracbits on velEst
  oldPos = posCtrl;
  
179
  // store velEst and ref to be sent/used here
180
181
182
  cli();
  velEstSend = velEst;
  refCtrl = ref;
183
  // ref = ref*(1-brake); // emergency stop
184
  sei();
185

186

187
  // control error
188
  e = refCtrl-((velEst+16)>>5);  // mm/s
189

190
191
  // temporary ctrl-signal
  v = (((K*e+(1<<5))>>6)+((I+(1<<3))>>4));
192
193

  // friction compensation
194
  if (refCtrl > 0) {
195
196
197
    v = v+fr_comp;
  } else if (refCtrl < 0) {
    v = v-fr_comp;
198
199
  }

200
201
202
203
204
205
206
207
208
209
210
211
212
213
  // variable that decides if I-part should be updated
  intCond = 1;
  
  // saturation of v
  if (v > V_MAX) {
    vSat = V_MAX;
    if (e > 0)
      intCond = 0;
  } else if (v < V_MIN) {
    vSat = V_MIN;
    if (e < 0)
      intCond = 0;
  } else {
    vSat = v;
214
  }
215
216
217
218
219
  
  if (intCond)
    I = I + (((Ke*e)+(1<<1))>>2);

  // scale ctrl-signal to send over twi
Pontus Giselsson's avatar
Pontus Giselsson committed
220
  u = (vSat+8)>>4; // u=127 gives current = 6.75 A, vSat makes u saturate at 114
221

222
  // u that is sent to simulink
223
224
225
  cli();
  uSend = u;
  sei();
226

Pontus Giselsson's avatar
Pontus Giselsson committed
227
228


229
  // TWI-communication to set current reference on the other atmel
230
231
  // send start command
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTA),TWCR);
Pontus Giselsson's avatar
Pontus Giselsson committed
232

233
  while (!(TWCR&BV(TWINT))) {}
234
    
Pontus Giselsson's avatar
Pontus Giselsson committed
235
236


237
238
239
240
  // Contact slave  
  outp(0x02,TWDR);  // slave is 0x02 (sla+w)
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
241
242
243
 
  // Send reference byte to slave
  outp((int8_t)u,TWDR);  // send 8 bits reference
244
245
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
Pontus Giselsson's avatar
Pontus Giselsson committed
246
    
247
248
249
250
251
252
253
254
255
256
257
  // stop transmission
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTO),TWCR);

}


int main()
{
  cli();
  
  //Port directions
258
  //outp(0x80,DDRB);   // Led output
259
  outp(0x10,DDRC);  // timer calculation port
260
261
262
  PORTD = 0x40;  // pull up on reset switch


263
264

  /* Timer section */
265
266
  // Enable timer0, timer1, timer2 compare match interrupts
  outp(BV(OCIE0)|BV(OCIE1A)|BV(OCIE2),TIMSK);
267
  
268
  /* Timer 2, 73 kHz Prescaler 1, encoder counter for position measurement */
269
270
271
272
  outp(BV(WGM21)|BV(CS20),TCCR2);
  outp(200,OCR2);
  /* Reset timer 2 */
  outp(0,TCNT2);
273
  
274
  /* Timer 1, 1 kHz , prescaler 1, ctrl-loop */
275
276
  outp(BV(WGM12)|BV(CS10),TCCR1B);
  outp(0x39,OCR1AH);
277
  outp(0x7f,OCR1AL);
278
279
280
281
  outp(0,TCNT1H);
  outp(0,TCNT1L);
  

Pontus Giselsson's avatar
Pontus Giselsson committed
282
  /* Timer 0, 40 kHz, Prescaler 8, serial communication */
283
  outp(BV(WGM01)|BV(CS01),TCCR0);
Pontus Giselsson's avatar
Pontus Giselsson committed
284
285
286
  //outp(184-1,OCR0); // 10 kHz
  outp(46-1,OCR0); // 40 kHz
   /* Reset timer 0 */
287
288
  outp(0,TCNT0);

289
290

  // syncronization (ctrl interrupt 34 micros before communication interrupt)
291
  TCNT1 = TCNT0*8+500;
292
293
  
  
294
  //Serial communication initialization
295
  outp(0x00, UCSRA);	// USART:
296
  outp(0x18, UCSRB);	// USART: RxEnable|TxEnable
297
298
299
300
301
  outp(0x86, UCSRC);	// USART: 8bit, no parity
  outp(0x00, UBRRH);	// USART: 115200 @ 14.7456MHz
  outp(7,UBRRL);	// USART: 115200 @ 14.7456MHz
  
  
302
303
  
  /* AREF (AREF is 5V) pin external capacitor, ADC3 for pendulum angle */
304
  outp(BV(REFS0)|BV(MUX0)|BV(MUX1),ADMUX);
305
  
306
  // Enable ADC on adc3, start first conversion, prescaler 128, free running mode
307
  outp(BV(ADEN)|BV(ADSC)|BV(ADATE)|BV(ADPS2)|BV(ADPS1)|BV(ADPS0),ADCSRA);
308
309
310
311
312
313
314
315


  // Initialize Master TWI
  outp(0x10,TWBR);  // set SCL-frequency CPU-freq/(16+2*16)
  outp(BV(TWEN),TWCR); // enable TWI

  // initialize position measurements
  initPos();
316

317
  // initialize pc-communication
318
  pccom_init();
319
320
321
322
323
  
  //Enable interrupts
  sei();

  // loop
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  while (1) {
    // reset position, velocity estimate and integral part of ctrl if reset button pushed
    if (!(PIND & 0x40)) {
      cli();
      low = inp(ADCL);
      high = inp(ADCH);
      pos = 0; // reset position
      angleOffset =  ((int16_t) ((high<<8) | low) - 512);
      //angleOffset = -150;
      sei();
      oldPos = 0;
      velEst = 0; // reset velocity estimate
      I = 0; // reset integral part of controller
      u = 0; // reset ctrl signal
    } 
  }
340
}