vel_control.c 8.18 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 = 800;       // 6 frac bits, prop constant
volatile int32_t Ke = 30;        // 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
volatile int16_t low;            // temporary variable for ad-reading
volatile int16_t high;           // temporary variable for ad-reading
volatile int16_t angleOffset = 0;

59

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


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

74
75

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

82
83

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

88

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

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

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

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

}


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

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

  sei(); // enable interrupts from encoder-counter
139

140
  // Poll UART receiver
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  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;
  }
156
  
157
  TIFR = (1<<OCF0);        // skip pending interrupts from serial comm, (but not from ctrl)
158

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

161
162
163
164
}



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

170
  sei(); // to enable interupts from encoder counter and communication
171

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

183

184
  // control error
185
  e = refCtrl-((velEst+16)>>5);  // mm/s
186

187
188
  // temporary ctrl-signal
  v = (((K*e+(1<<5))>>6)+((I+(1<<3))>>4));
189
190

  // friction compensation
191
  if (refCtrl > 0) {
192
193
194
    v = v+fr_comp;
  } else if (refCtrl < 0) {
    v = v-fr_comp;
195
196
  }

197
198
199
200
201
202
203
204
205
206
207
208
209
210
  // 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;
211
  }
212
213
214
215
216
  
  if (intCond)
    I = I + (((Ke*e)+(1<<1))>>2);

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

219
  // u that is sent to simulink
220
221
222
  cli();
  uSend = u;
  sei();
223

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

}


int main()
{
  cli();
  
  //Port directions
251
  //outp(0x80,DDRB);   // Led output
252
  outp(0x10,DDRC);  // timer calculation port
253
254
255
  PORTD = 0x40;  // pull up on reset switch


256
257

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

275
  /* Timer 0, 10 kHz, Prescaler 8, serial communication */
276
277
  outp(BV(WGM01)|BV(CS01),TCCR0);
  outp(184-1,OCR0); // 10 kHz
278
279
280
  /* Reset timer 0 */
  outp(0,TCNT0);

281
282

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


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

  // initialize position measurements
  initPos();
308

309
  // initialize pc-communication
310
  pccom_init();
311
312
313
314
315
  
  //Enable interrupts
  sei();

  // loop
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  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
    } 
  }
332
}