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


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


12
13
14
// reference variables
volatile int32_t ref = 0;  // 11 frac bits
volatile int16_t refFlag = 0;
15
volatile int16_t deltaRef = 1;
16
volatile int16_t refCount = 0;
17
volatile int32_t refTest = 0;
18
19
20
21
22
23
24
25
26
27
28
29
30


// velocity control variables
volatile int32_t u = 0;  // 11 frac bits
volatile int32_t v = 0;  // 11 frac bits
volatile int8_t brake = 0;
volatile int32_t I = 0; // 11 frac bits
volatile int32_t e = 0; // 11 frac bits
volatile int32_t K = 1200; // 6 frac bits
volatile int32_t Ke = 45; // 6 frac bits
volatile int32_t Ksat = 3; // 6 frac bits

// encoder variables
31
32
33
34
35
//#define ENCODERY  (PIND&(uint8_t)(1<<2))        //Positional encoder pins
//#define ENCODERX  (PINB&(uint8_t)(1<<1))        //Positional encoder pins
#define ENCODERY  (PIND&0x04)        //Positional encoder pins
#define ENCODERX  ((PINB&0x02)<<1)        //Positional encoder pins

36
volatile int16_t pos = 0;
37
volatile int16_t posTemp = 0;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
volatile int16_t oldPos = 0;
volatile int8_t newX;
volatile int8_t newY;
volatile int8_t oldX;
volatile int8_t oldY;
volatile int8_t sum = 0;

// velocity estimation parameters
volatile int32_t velEst = 0; // 5 frac bits
volatile int32_t velEstTemp = 0; 
int16_t a = 116; //7 frac bits
int16_t b = 152; // 5 frac bits


52
53
// return position (in tics) 
int32_t getPosition() {
54
55
  posTemp = pos;
  return ((int32_t) posTemp);
56
57
58
59
60
61
62
63
}


// return velocity (in mm/s) 
int32_t getVelocity() {
  return velEst;
}

64
65
66
67
68
69
70
71

/* Routine used to set the red LED */
void setLED(uint8_t on)
{
  if (on) PORTB &= ~0x80; //Turn on
  else PORTB |= 0x80;     //Turn off
}

72
73
74
75
76
77
// Set new reference value
void setRef(int32_t newRef) {
  ref = newRef;
}


78
79
80
81
82
83
84
85
86
87
88
89
90
/* Routine used to initialize the positional encoders */
void initPos()
{
  oldX = ENCODERX;
  oldY = ENCODERY;
}

/* Routine used to track the cart position */
void setPos()
{

}

91
/* Timer 2, Encoder counter, 73 kHz */
92
SIGNAL(SIG_OUTPUT_COMPARE2) {
93
94
95

   

96
97
98
  // Update position from encoder
  newX = ENCODERX;
  newY = ENCODERY;
99
100
  if((newX != oldX) || (newY != oldY)) {                            //Check if any value changed
      /*
101
102
      sum = (oldX<<2)+oldY+newX+(newY>>2);                    //Find state
      if (sum == 2 || sum == 4 || sum == 11 || sum == 13) {      //Predetermined values determine direction
103
	pos = pos+1;
104
      } else if (sum == 1 || sum == 7 || sum ==  8 || sum == 14) {
105
106
107
108
109
110
111
112
113
	pos = pos-1;
      } else {
	brake = 1; // emergency brake
      }
      */
      if ((oldX == newY) && (oldY != newX)) {
	pos = pos+1;
      } else if ((oldX != newY) && (oldY == newX)) {
	pos = pos-1;
114
115
116
117
118
      } else {
	brake = 1;
      }
      oldX = newX;
      oldY = newY;
119
120
121
122
123
124
125
126
127
128
129
  }



}


/* Timer 1, serial communication */
SIGNAL(SIG_OUTPUT_COMPARE0) {

  TIMSK &= ~(BV(OCIE1A)|BV(OCIE0));
130

131
132
133
  sei(); // enable interrupts from timer 0 and timer 2
  
  //PORTC |= 0x10;  // to clock calulation time
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  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;
  }
  //PORTC &= ~0x10; 
151
  
152
153
  TIMSK |= (BV(OCIE1A)|BV(OCIE0)); 
 
154
155
156
157
}



158
159
/* Timer 0, control loop , 1 kHz */
SIGNAL(SIG_OUTPUT_COMPARE1A) {
160

161
162
  TIMSK &= ~(BV(OCIE0)|BV(OCIE1A));
  
163
164
  sei(); // to enable interupts from timer2

165
   
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

  // linear velocity estimator
  // velocity estimate in mm/s 
  velEst = (((a*velEst+64)>>7)+b*(pos-oldPos));  // 5 fracbits on velEst
  oldPos = pos;

  // control error
  //e = ref-((int16_t)((velEst+16)>>5));  // mm/s
  e = ref-((velEst+16)>>5);  // mm/s
  
  v = (((K*e+(1<<5))>>6)+((I+(1<<3))>>4));

  //saturation and update integral part of ctrl
  if (v > 2047) {
    I = I + ((((Ke*e) + (Ksat)*(2047-v))+(1<<1))>>2);
  } else if (v < -2048) {
    I = I + ((((Ke*e) + (Ksat)*(-2048-v))+(1<<1))>>2);
  } else {
    I = I + ((Ke*e+(1<<3))>>2);
  }
  
  u = (v+8)>>4; //8 frac bits to current loop

  // friction compensation
  if (ref > 0) {
    u = u+10;
  } else if (ref < 0) {
    u = u-10;
  }

  // Saturation
  if (u > 127) {
    u = 127;
  } else if (u < -128) {
    u = -128;
  }

203
204
205
  //u = 2;

  /*
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  // reference calculations
  refCount++;
  
  
  if (refFlag == 0) {
    if  (refCount%1 == 0)
      ref = ref+deltaRef;
    if (refCount == 500) {
      refFlag = 1;
      deltaRef = -deltaRef;
      refCount = 0;
    }
  } else {
    if  (refCount%1 == 0)
      ref = ref+deltaRef;
    if (refCount == 1000) {
      refCount = 0;
      deltaRef = -deltaRef;
    }
  }
226
  */
227
228
229
230
231
232
233
234

  /*
  if (refCount == 1000) {
    ref = 0;
  }
  */

  
235
  ref = ref*(1-brake);
236
237

  
238

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  // TWI-communication
  // send start command
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTA),TWCR);
  while (!(TWCR&BV(TWINT))) {}
  
  
  
  // Contact slave  
  outp(0x02,TWDR);  // slave is 0x02 (sla+w)
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
  
   
  // Send reference byte
  outp((int8_t)(u&0x000000ff),TWDR);  // send 8 bits reference
  outp(BV(TWINT)|BV(TWEN),TWCR);
  while (!(TWCR&BV(TWINT))) {}
  
  
  // stop transmission
  outp(BV(TWINT)|BV(TWEN)|BV(TWSTO),TWCR);

261
 
262

263
  TIMSK |= (BV(OCIE0)|BV(OCIE1A));
264
265


266

267
268
269
270
271
272
273
274
275
276
277
278
}


int main()
{
  cli();
  
  //Port directions
  outp(0x80,DDRB);   // Led output
  outp(0x10,DDRC);  // timer calculation port

  /* Timer section */
279
280
  // Enable timer0, timer1, timer2 compare match interrupts
  outp(BV(OCIE0)|BV(OCIE1A)|BV(OCIE2),TIMSK);
281
  
282
  /* Timer 2, 73 kHz Prescaler 1 */
283
284
285
286
  outp(BV(WGM21)|BV(CS20),TCCR2);
  outp(200,OCR2);
  /* Reset timer 2 */
  outp(0,TCNT2);
287
288
289
290
291
292
293
294
295
296
  
  // --------old-----------------------------------------------
  /* Timer 1, Prescaler 1, 73/8 kHz*/
  
  //outp(BV(WGM12)|BV(CS10),TCCR1B);
  //outp(0x06,OCR1AH);
  //outp(0x40,OCR1AL);
  //outp(0,TCNT1H);
  //outp(0,TCNT1L);
  
297
298

  /* Timer 0, 1 kHz Prescaler 64 */
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  
  //outp(BV(WGM01)|BV(CS01)|BV(CS00),TCCR0);
  //outp(230,OCR0);
  
  /* Reset timer 0 */
  //outp(0,TCNT0);
  
  // --------new-----------------------------------------------
  /* Timer 1, 1 kHz */
  outp(BV(WGM12)|BV(CS10),TCCR1B);
  outp(0x39,OCR1AH);
  outp(0x99,OCR1AL);
  outp(0,TCNT1H);
  outp(0,TCNT1L);
  

  /* Timer 0, 10 kHz, Prescaler 64 */
316
  outp(BV(WGM01)|BV(CS01)|BV(CS00),TCCR0);
317
318
319
  //outp(200,OCR0);
  outp(23,OCR0); // 10 kHz

320
321
  /* Reset timer 0 */
  outp(0,TCNT0);
322
  //----------------------------------------------------------
323
324

  
325
326
  
  
327
328
  //Serial communication
  outp(0x00, UCSRA);	// USART:
329
  outp(0x18, UCSRB);	// USART: RxEnable|TxEnable
330
331
332
333
334
335
336
  outp(0x86, UCSRC);	// USART: 8bit, no parity
  outp(0x00, UBRRH);	// USART: 115200 @ 14.7456MHz
  outp(7,UBRRL);	// USART: 115200 @ 14.7456MHz
  

  
  /* AREF (AREF is 5V) pin external capacitor, MUX0 for current */
337
  //outp(BV(REFS0)|BV(MUX3),ADMUX);
338
  
339
340
  // Enable ADC, start first conversion, prescaler 32, free running mode
  //outp(BV(ADEN)|BV(ADSC)|BV(ADPS2)|BV(ADPS0),ADCSRA);
341
342
343
344
345
346
347
348


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

  // initialize position measurements
  initPos();
349
350

  pccom_init();
351
352
353
354
355
356
357
  
  //Enable interrupts
  sei();

  // loop
  while (1) {}
}