vel_control.c 8.43 KB
Newer Older
1
2
3
4
5
6
7
/***************************************************************

 Current regulation - Pontus Giselsson, Per-Ola Larsson 18/02/09
           for LTH - reglerteknik

/***************************************************************

8
9
10
11
12
13
#include <avr/twi.h>
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>

14
15
16
17
#include "pccom.h"
#include "vel_control.h"


18
// reference variables
19
20
volatile int32_t ref = 0;        // 11 frac bits
volatile int32_t refCtrl = 0;    // ref used in ctrl-loop (=ref sent from simulink)
21
22
23


// velocity control variables
24
25
26
27
28
29
30
31
32
33
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
34
35
volatile int32_t K = 807;       // 6 frac bits, prop constant
volatile int32_t Ke = 13;        // 6 frac bits, integral constant
36
volatile int8_t fr_comp = (10<<3);
Pontus Giselsson's avatar
Pontus Giselsson committed
37
38
#define V_MAX (120<<4)
#define V_MIN (-120<<4)
39

40
41

// encoder variables
42
#define ENCODERY  (PIND&0x04)        //Positional encoder pins
43
44
45
46
47
48
49
50
51
52
53
#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
54
55

// velocity estimation parameters
56
57
58
59
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)
60

61
// adc measurement variables
62
63
64
65
66
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) */
67
int32_t getPosition() {
68
  cli();
69
  posTemp = pos;
70
  sei();
71
  return ((int32_t) posTemp);
72
73
74
}


75
/* return velocity (in mm/s) */
76
int32_t getVelocity() {
77
78
79
  return velEstSend;
}

80
81

/* return last angle measurement */
82
83
84
int16_t getAngle() {
  low = inp(ADCL);
  high = inp(ADCH);
85
  return (((int16_t) ((high<<8) | low) - 512)-angleOffset);
86
87
}

88
89

/* return current-reference */
90
91
int32_t getCurrentRef() {
  return uSend;
92
93
}

94

95
/* Set new reference value */
96
97
98
99
void setRef(int32_t newRef) {
  ref = newRef;
}

100
101
102
103
104
105
106
/* Routine used to initialize the positional encoders */
void initPos()
{
  oldX = ENCODERX;
  oldY = ENCODERY;
}

107
/* Timer 2, Encoder counter, 73 kHz updates position variable */
108
SIGNAL(SIG_OUTPUT_COMPARE2) {
109

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

}


139
/* Timer 0, serial communication with simulink */
140
141
SIGNAL(SIG_OUTPUT_COMPARE0) {

142
143
144
  TIMSK &= ~(BV(OCIE0)|BV(OCIE1A));   // Disable communication and ctrl-interrupts

  sei(); // enable interrupts from encoder-counter
Pontus Giselsson's avatar
Pontus Giselsson committed
145
  
146
  // Poll UART receiver
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  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
162

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

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

167
168
169
170
}



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

176
  sei(); // to enable interupts from encoder counter and communication
177

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

  // control error
190
  e = refCtrl-((velEst+16)>>5);  // mm/s
191

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

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

202
203
204
205
206
207
208
209
210
211
212
213
214
215
  // 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;
216
  }
217
218
219
220
221
  
  if (intCond)
    I = I + (((Ke*e)+(1<<1))>>2);

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

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

Pontus Giselsson's avatar
Pontus Giselsson committed
229

230
  // TWI-communication to set current reference on the other atmel
231
232
233
  // send start command
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTA),TWCR);
  while (!(TWCR&BV(TWINT))) {}
234
    
235
236
237
238
  // Contact slave  
  outp(0x02,TWDR);  // slave is 0x02 (sla+w)
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
239
240
241
 
  // Send reference byte to slave
  outp((int8_t)u,TWDR);  // send 8 bits reference
242
243
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
Pontus Giselsson's avatar
Pontus Giselsson committed
244
    
245
246
247
248
249
250
251
252
253
254
255
  // stop transmission
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTO),TWCR);

}


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


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

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

286
287

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


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

  // initialize position measurements
  initPos();
312

313
  // initialize pc-communication
314
  pccom_init();
315
316
317
318
319
  
  //Enable interrupts
  sei();

  // loop
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  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
    } 
  }
336
}