vel_control.c 6.42 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
31
32
33


// 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
#define ENCODERY  (PIND&(uint8_t)(1<<2))        //Positional encoder pins
#define ENCODERX  (PINB&(uint8_t)(1<<1))        //Positional encoder pins
volatile int16_t pos = 0;
34
volatile int16_t posTemp = 0;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
volatile int16_t oldPos = 0;
volatile int16_t deltaPos = 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


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


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

62
63
64
65
66
67
68
69

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

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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Routine used to initialize the positional encoders */
void initPos()
{
  oldX = ENCODERX;
  oldY = ENCODERY;
}

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

}

/* Timer 2, Encoder */
SIGNAL(SIG_OUTPUT_COMPARE2) {
  
  deltaPos = 0;
  // Update position from encoder
  newX = ENCODERX;
  newY = ENCODERY;
  if((newX != oldX) || (newY != oldY))                            //Check if any value changed
    {
      sum = (oldX<<2)+oldY+newX+(newY>>2);                    //Find state
      if (sum == 2 || sum == 4 || sum == 11 || sum == 13) {      //Predetermined values determine direction
	//pos = pos+1;
	deltaPos = 1;
      } else if (sum == 1 || sum == 7 || sum ==  8 || sum == 14) {
	//pos = pos-1;
	deltaPos = -1;
      } else {
	brake = 1;
	// emergency brake
      }
      oldX = newX;
      oldY = newY;
    }


  // velocity estimation cut-off frequency 500 Hz
  pos = pos+deltaPos;    // update oldPos, 0 frac bits on pos and oldPos
  
}



/* Timer 0, control loop */
SIGNAL(SIG_OUTPUT_COMPARE0) {

  sei(); // to enable interupts from timer2

126
127
  TIMSK &= ~BV(OCIE0);

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  PORTC |= 0x10;  // to clock calulation time

  // 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;
  }

166
167
168
  //u = 2;

  /*
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  // 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;
    }
  }
189
  */
190
191
192
193
194
195
196
197

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

  
198
  ref = ref*(1-brake);
199
200

  
201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  // 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);

224
225
226



227
228
  // ------- Noncritical section -------

229
  
230
  // Poll UART receiver
231
  
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  int8_t tempVar = 0;
  for (tempVar = 0;tempVar < 10; tempVar++) {
    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;
      while (toSend >= 0) {
	UDR = (char)toSend;
	while ((UCSRA & (1<<UDRE)) == 0) {} // wait for previous send
	toSend = pccom_getNextByteToSend();
      }
253
254
    }
  }
255
256
257
258
  
    
  PORTC &= ~0x10;
  TIMSK |= BV(OCIE0);
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
}


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

  /* Timer section */
  // Enable timer2 compare match interrupts
  outp(BV(OCIE0)|BV(OCIE2),TIMSK);
  
  /* Timer 2, 59 kHz Prescaler 1 */
  outp(BV(WGM21)|BV(CS20),TCCR2);
  outp(200,OCR2);
  /* Reset timer 2 */
  outp(0,TCNT2);

  /* Timer 0, 1 kHz Prescaler 64 */
  outp(BV(WGM01)|BV(CS01)|BV(CS00),TCCR0);
  outp(230,OCR0);
  /* Reset timer 0 */
  outp(0,TCNT0);

  
  //Serial communication
  outp(0x00, UCSRA);	// USART:
289
  outp(0x18, UCSRB);	// USART: RxEnable|TxEnable
290
291
292
293
294
295
296
  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 */
297
  //outp(BV(REFS0)|BV(MUX3),ADMUX);
298
  
299
300
  // Enable ADC, start first conversion, prescaler 32, free running mode
  //outp(BV(ADEN)|BV(ADSC)|BV(ADPS2)|BV(ADPS0),ADCSRA);
301
302
303
304
305
306
307
308


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

  // initialize position measurements
  initPos();
309
310

  pccom_init();
311
312
313
314
315
316
317
  
  //Enable interrupts
  sei();

  // loop
  while (1) {}
}