]> defiant.homedns.org Git - ros_wild_thumper.git/blob - avr/motor_ctrl/main.c
avr: added speed & pose calculations
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <math.h>
5 #include <avr/io.h>
6 #include <avr/interrupt.h>
7 #include <avr/sleep.h>
8 #include "uart.h"
9
10 /*
11  * I2C Register Map (8 Bit)
12  * 0x00 Register select
13  * 0x01 Motor 1 PWM MSB
14  * 0x02 Motor 1 PWM LSB
15  * 0x03 Motor 2 PWM MSB
16  * 0x04 Motor 2 PWM LSB
17  * 0x05 Motor 3 PWM MSB
18  * 0x06 Motor 3 PWM LSB
19  * 0x07 Motor 4 PWM MSB
20  * 0x08 Motor 4 PWM LSB
21  * free
22  * 0x10 Hall 1 MSB
23  * 0x11 Hall 1 LSB
24  * 0x12 Hall 2 MSB
25  * 0x13 Hall 2 LSB
26  * 0x14 Hall 3 MSB
27  * 0x15 Hall 3 LSB
28  * 0x16 Hall 4 MSB
29  * 0x17 Hall 4 LSB
30  * free
31  * 0x20 Motor 1 speed wish MSB
32  * 0x21 Motor 1 speed wish LSB
33  * 0x22 Motor 2 speed wish MSB
34  * 0x23 Motor 2 speed wish LSB
35  * 0x24 Motor 3 speed wish MSB
36  * 0x25 Motor 3 speed wish LSB
37  * 0x26 Motor 4 speed wish MSB
38  * 0x27 Motor 4 speed wish LSB
39  * 0x28 Left speed wish (m/s) MSB
40  * 0x29 Left speed wish (m/s)
41  * 0x2A Left speed wish (m/s)
42  * 0x2B Left speed wish (m/s) LSB
43  * 0x2C Right speed wish (m/s) MSB
44  * 0x2D Right speed wish (m/s)
45  * 0x2E Right speed wish (m/s)
46  * 0x2F Right speed wish (m/s) LSB
47  * 0x30 Motor 1 speed MSB
48  * 0x31 Motor 1 speed LSB
49  * 0x32 Motor 2 speed MSB
50  * 0x33 Motor 2 speed LSB
51  * 0x34 Motor 3 speed MSB
52  * 0x35 Motor 3 speed LSB
53  * 0x36 Motor 4 speed MSB
54  * 0x37 Motor 4 speed LSB
55  * 0x38 Speed (m/s) MSB
56  * 0x39 Speed (m/s)
57  * 0x3A Speed (m/s)
58  * 0x3B Speed (m/s) LSB
59  * 0x3C Angle (rad/s) MSB
60  * 0x3D Angle (rad/s)
61  * 0x3E Angle (rad/s)
62  * 0x3F Angle (rad/s) LSB
63  * free
64  * 0x40 Position x (m) MSB
65  * 0x41 Position x (m)
66  * 0x42 Position x (m)
67  * 0x43 Position x (m) LSB
68  * 0x44 Position y (m) MSB
69  * 0x45 Position y (m)
70  * 0x46 Position y (m)
71  * 0x47 Position y (m) LSB
72  * 0x48 Position angle MSB
73  * 0x49 Position angle
74  * 0x4A Position angle
75  * 0x4B Position angle LSB
76  * free
77  * 0x50 speed wish (m/s) MSB
78  * 0x51 speed wish (m/s)
79  * 0x52 speed wish (m/s)
80  * 0x53 speed wish (m/s) LSB
81  * 0x54 angle wish (rad/s) MSB
82  * 0x55 angle wish (rad/s)
83  * 0x56 angle wish (rad/s)
84  * 0x57 angle wish (rad/s) LSB
85  * free
86  * 0x90 Motor 1 switch
87  * 0x91 Motor 2 switch
88  * 0x92 Motor 3 switch
89  * 0x93 Motor 4 switch
90  * 0x94 TLE Error status
91  * free
92  * 0xff Bootloader
93  */
94
95
96 #define TWI_ACK         TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
97 #define TWI_RESET       TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
98 #define TWI_NAK         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
99
100 #define KP 0.009
101 #define KI 0.051429
102 #define KD 0.000378
103 #define PID_T 0.01
104 #define STEP_PER_M 3376.1 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34
105 #define WHEEL_DIST 0.252
106
107 enum mode {
108         MOTOR_MANUAL,
109         MOTOR_PID
110 };
111
112 typedef union {
113         float f;
114         uint32_t i;
115 } ufloat_t;
116
117 static volatile uint8_t ireg=0;
118 static volatile uint8_t bootloader=0;
119 static volatile int16_t motor1=0; // -255..+255
120 static volatile int16_t motor2=0;
121 static volatile int16_t motor3=0;
122 static volatile int16_t motor4=0;
123 static volatile int16_t pos1=0; // step
124 static volatile int16_t pos2=0;
125 static volatile int16_t pos3=0;
126 static volatile int16_t pos4=0;
127 static volatile enum mode motor1_mode=MOTOR_MANUAL;
128 static volatile enum mode motor2_mode=MOTOR_MANUAL;
129 static volatile enum mode motor3_mode=MOTOR_MANUAL;
130 static volatile enum mode motor4_mode=MOTOR_MANUAL;
131 static volatile uint8_t motor1_switch=0;
132 static volatile uint8_t motor2_switch=0;
133 static volatile uint8_t motor3_switch=0;
134 static volatile uint8_t motor4_switch=0;
135 static volatile int16_t speed1_wish=0; // step/s
136 static volatile int16_t speed2_wish=0;
137 static volatile int16_t speed3_wish=0;
138 static volatile int16_t speed4_wish=0;
139 static volatile uint8_t run_update=0;
140 static volatile int16_t speed1=0; // step/s
141 static volatile int16_t speed2=0;
142 static volatile int16_t speed3=0;
143 static volatile int16_t speed4=0;
144 static volatile ufloat_t pos_x={0.0};
145 static volatile ufloat_t pos_y={0.0};
146 static volatile ufloat_t angle={0.0};
147
148 ISR(TWI_vect)
149 {
150         static uint8_t tmp=0;
151         static int16_t tmp16=0;
152         static ufloat_t tmp_speed;
153         static ufloat_t tmp_angle;
154
155         switch (TWSR & 0xF8)
156         {  
157                 case 0x60: // start write
158                         TWI_ACK;
159                         ireg = 0;
160                         break;
161                 case 0x80: // write
162                         switch(ireg) {
163                                 case 0x00: // register select
164                                         ireg = TWDR;
165                                         ireg--; // because we do ireg++ below
166                                         TWI_ACK;
167                                         break;
168                                 case 0x01: // Motor 1 MSB
169                                         tmp = TWDR;
170                                         TWI_ACK;
171                                         break;
172                                 case 0x02: // Motor 1 LSB
173                                         motor1 = tmp<<8 | TWDR;
174                                         motor1_mode = MOTOR_MANUAL;
175                                         TWI_ACK;
176                                         break;
177                                 case 0x03: // Motor 2 MSB
178                                         tmp = TWDR;
179                                         TWI_ACK;
180                                         break;
181                                 case 0x04: // Motor 2 LSB
182                                         motor2 = tmp<<8 | TWDR;
183                                         motor2_mode = MOTOR_MANUAL;
184                                         TWI_ACK;
185                                         break;
186                                 case 0x05: // Motor 3 MSB
187                                         tmp = TWDR;
188                                         TWI_ACK;
189                                         break;
190                                 case 0x06: // Motor 3 LSB
191                                         motor3 = tmp<<8 | TWDR;
192                                         motor3_mode = MOTOR_MANUAL;
193                                         TWI_ACK;
194                                         break;
195                                 case 0x07: // Motor 4 MSB
196                                         tmp = TWDR;
197                                         TWI_ACK;
198                                         break;
199                                 case 0x08: // Motor 4 LSB
200                                         motor4 = tmp<<8 | TWDR;
201                                         motor4_mode = MOTOR_MANUAL;
202                                         TWI_ACK;
203                                         break;
204                                 case 0x20: // Motor 1 speed wish MSB
205                                         tmp = TWDR;
206                                         TWI_ACK;
207                                         break;
208                                 case 0x21: // Motor 1 speed wish LSB
209                                         speed1_wish = tmp<<8 | TWDR;
210                                         motor1_mode = MOTOR_PID;
211                                         TWI_ACK;
212                                         break;
213                                 case 0x22: // Motor 2 speed wish MSB
214                                         tmp = TWDR;
215                                         TWI_ACK;
216                                         break;
217                                 case 0x23: // Motor 2 speed wish LSB
218                                         speed2_wish = tmp<<8 | TWDR;
219                                         motor2_mode = MOTOR_PID;
220                                         TWI_ACK;
221                                         break;
222                                 case 0x24: // Motor 3 speed wish MSB
223                                         tmp = TWDR;
224                                         TWI_ACK;
225                                         break;
226                                 case 0x25: // Motor 3 speed wish LSB
227                                         speed3_wish = tmp<<8 | TWDR;
228                                         motor3_mode = MOTOR_PID;
229                                         TWI_ACK;
230                                         break;
231                                 case 0x26: // Motor 4 speed wish MSB
232                                         tmp = TWDR;
233                                         TWI_ACK;
234                                         break;
235                                 case 0x27: // Motor 4 speed wish LSB
236                                         speed4_wish = tmp<<8 | TWDR;
237                                         motor4_mode = MOTOR_PID;
238                                         TWI_ACK;
239                                         break;
240                                 case 0x28: // Left speed wish MSB
241                                         tmp_speed.i = TWDR;
242                                         TWI_ACK;
243                                         break;
244                                 case 0x29: // Left speed wish
245                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
246                                         TWI_ACK;
247                                         break;
248                                 case 0x2A: // Left speed wish
249                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
250                                         TWI_ACK;
251                                         break;
252                                 case 0x2B: // Left speed wish LSB
253                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
254                                         speed1_wish = tmp_speed.f*STEP_PER_M;
255                                         speed2_wish = tmp_speed.f*STEP_PER_M;
256                                         motor1_mode = MOTOR_PID;
257                                         motor2_mode = MOTOR_PID;
258                                         TWI_ACK;
259                                         break;
260                                 case 0x2C: // Right speed wish MSB
261                                         tmp_speed.i = TWDR;
262                                         TWI_ACK;
263                                         break;
264                                 case 0x2D: // Right speed wish
265                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
266                                         TWI_ACK;
267                                         break;
268                                 case 0x2E: // Right speed wish
269                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
270                                         TWI_ACK;
271                                         break;
272                                 case 0x2F: // Right speed wish LSB
273                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
274                                         speed1_wish = tmp_speed.f*STEP_PER_M;
275                                         speed2_wish = tmp_speed.f*STEP_PER_M;
276                                         motor1_mode = MOTOR_PID;
277                                         motor2_mode = MOTOR_PID;
278                                         TWI_ACK;
279                                         break;
280                                 case 0x50: // speed wish MSB
281                                         tmp_speed.i = TWDR;
282                                         TWI_ACK;
283                                         break;
284                                 case 0x51: // speed wish
285                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
286                                         TWI_ACK;
287                                         break;
288                                 case 0x52: // speed wish
289                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
290                                         TWI_ACK;
291                                         break;
292                                 case 0x53: // speed wish LSB
293                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
294                                         TWI_ACK;
295                                         break;
296                                 case 0x54: // angle wish MSB
297                                         tmp_angle.i = TWDR;
298                                         TWI_ACK;
299                                         break;
300                                 case 0x55: // angle wish
301                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
302                                         TWI_ACK;
303                                         break;
304                                 case 0x56: // angle wish
305                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
306                                         TWI_ACK;
307                                         break;
308                                 case 0x57: // angle wish LSB
309                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
310                                         {
311                                         float speed_wish_right = tmp_angle.f*M_PI*WHEEL_DIST/2 + tmp_speed.f;
312                                         float speed_wish_left = tmp_speed.f*2-speed_wish_right;
313                                         speed1_wish = speed_wish_left*STEP_PER_M;
314                                         speed2_wish = speed_wish_left*STEP_PER_M;
315                                         speed3_wish = speed_wish_right*STEP_PER_M;
316                                         speed4_wish = speed_wish_right*STEP_PER_M;
317                                         }
318                                         motor1_mode = MOTOR_PID;
319                                         motor2_mode = MOTOR_PID;
320                                         motor3_mode = MOTOR_PID;
321                                         motor4_mode = MOTOR_PID;
322                                         TWI_ACK;
323                                         break;
324                                 case 0x90: // Motor 1 switch
325                                         motor1_switch = TWDR;
326                                         TWI_ACK;
327                                         break;
328                                 case 0x91: // Motor 2 switch
329                                         motor2_switch = TWDR;
330                                         TWI_ACK;
331                                         break;
332                                 case 0x92: // Motor 3 switch
333                                         motor3_switch = TWDR;
334                                         TWI_ACK;
335                                         break;
336                                 case 0x93: // Motor 4 switch
337                                         motor4_switch = TWDR;
338                                         TWI_ACK;
339                                         break;
340                                 case 0xff: // bootloader
341                                         bootloader = TWDR;
342                                 default:
343                                         TWI_NAK;
344                         }
345                         ireg++;
346                         break;
347                 case 0xA8: // start read
348                 case 0xB8: // read
349                         switch(ireg) {
350                                 case 0x02: // Motor 1 PWM
351                                         TWDR = OCR1A;
352                                         TWI_ACK;
353                                         break;
354                                 case 0x04: // Motor 2 PWM
355                                         TWDR = OCR1B;
356                                         TWI_ACK;
357                                         break;
358                                 case 0x06: // Motor 3 PWM
359                                         TWDR = OCR2;
360                                         TWI_ACK;
361                                         break;
362                                 case 0x08: // Motor 4 PWM
363                                         TWDR = OCR0;
364                                         TWI_ACK;
365                                         break;
366                                 case 0x10: // Hall 1 MSB
367                                         tmp16 = pos1;
368                                         TWDR = tmp16>>8;
369                                         TWI_ACK;
370                                         break;
371                                 case 0x11: // Hall 1 LSB
372                                         TWDR = tmp16;
373                                         TWI_ACK;
374                                         break;
375                                 case 0x12: // Hall 2 MSB
376                                         tmp16 = pos2;
377                                         TWDR = tmp16>>8;
378                                         TWI_ACK;
379                                         break;
380                                 case 0x13: // Hall 2 LSB
381                                         TWDR = tmp16;
382                                         TWI_ACK;
383                                         break;
384                                 case 0x14: // Hall 3 MSB
385                                         tmp16 = pos3;
386                                         TWDR = tmp16>>8;
387                                         TWI_ACK;
388                                         break;
389                                 case 0x15: // Hall 3 LSB
390                                         TWDR = tmp16;
391                                         TWI_ACK;
392                                         break;
393                                 case 0x16: // Hall 4 MSB
394                                         tmp16 = pos4;
395                                         TWDR = tmp16>>8;
396                                         TWI_ACK;
397                                         break;
398                                 case 0x17: // Hall 4 LSB
399                                         TWDR = tmp16;
400                                         TWI_ACK;
401                                         break;
402                                 case 0x20: // Motor 1 speed wish MSB
403                                         TWDR = speed1_wish>>8;
404                                         TWI_ACK;
405                                         break;
406                                 case 0x21: // Motor 1 speed wish LSB
407                                         TWDR = speed1_wish;
408                                         TWI_ACK;
409                                         break;
410                                 case 0x22: // Motor 2 speed wish MSB
411                                         TWDR = speed2_wish>>8;
412                                         TWI_ACK;
413                                         break;
414                                 case 0x23: // Motor 2 speed wish LSB
415                                         TWDR = speed2_wish;
416                                         TWI_ACK;
417                                         break;
418                                 case 0x24: // Motor 3 speed wish MSB
419                                         TWDR = speed3_wish>>8;
420                                         TWI_ACK;
421                                         break;
422                                 case 0x25: // Motor 3 speed wish LSB
423                                         TWDR = speed3_wish;
424                                         TWI_ACK;
425                                         break;
426                                 case 0x26: // Motor 4 speed wish MSB
427                                         TWDR = speed4_wish>>8;
428                                         TWI_ACK;
429                                         break;
430                                 case 0x27: // Motor 4 speed wish LSB
431                                         TWDR = speed4_wish;
432                                         TWI_ACK;
433                                         break;
434                                 case 0x30: // Motor 1 speed MSB
435                                         TWDR = speed1>>8;
436                                         TWI_ACK;
437                                         break;
438                                 case 0x31: // Motor 1 speed LSB
439                                         TWDR = speed1;
440                                         TWI_ACK;
441                                         break;
442                                 case 0x32: // Motor 2 speed MSB
443                                         TWDR = speed2>>8;
444                                         TWI_ACK;
445                                         break;
446                                 case 0x33: // Motor 2 speed LSB
447                                         TWDR = speed2;
448                                         TWI_ACK;
449                                         break;
450                                 case 0x34: // Motor 3 speed MSB
451                                         TWDR = speed3>>8;
452                                         TWI_ACK;
453                                         break;
454                                 case 0x35: // Motor 3 speed LSB
455                                         TWDR = speed3;
456                                         TWI_ACK;
457                                         break;
458                                 case 0x36: // Motor 4 speed MSB
459                                         TWDR = speed4>>8;
460                                         TWI_ACK;
461                                         break;
462                                 case 0x37: // Motor 4 speed LSB
463                                         TWDR = speed4;
464                                         TWI_ACK;
465                                         break;
466                                 case 0x38: // speed MSB
467                                         {
468                                         int16_t speed_l = (speed3+speed4)/2;
469                                         int16_t speed_r = (speed1+speed2)/2;
470                                         tmp_speed.f = (speed_l + speed_r)/(2.0*STEP_PER_M);
471                                         tmp_angle.f = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M);
472                                         }
473                                         TWDR = tmp_speed.i>>24;
474                                         TWI_ACK;
475                                         break;
476                                 case 0x39: // speed
477                                         TWDR = tmp_speed.i>>16;
478                                         TWI_ACK;
479                                         break;
480                                 case 0x3A: // speed
481                                         TWDR = tmp_speed.i>>8;
482                                         TWI_ACK;
483                                         break;
484                                 case 0x3B: // speed LSB
485                                         TWDR = tmp_speed.i;
486                                         TWI_ACK;
487                                         break;
488                                 case 0x3C: // angle MSB
489                                         TWDR = tmp_angle.i>>24;
490                                         TWI_ACK;
491                                         break;
492                                 case 0x3D: // angle
493                                         TWDR = tmp_angle.i>>16;
494                                         TWI_ACK;
495                                         break;
496                                 case 0x3E: // angle
497                                         TWDR = tmp_angle.i>>8;
498                                         TWI_ACK;
499                                         break;
500                                 case 0x3F: // angle LSB
501                                         TWDR = angle.i;
502                                         TWI_ACK;
503                                         break;
504                                 case 0x40: // Position x MSB
505                                         TWDR = pos_x.i>>24;
506                                         TWI_ACK;
507                                         break;
508                                 case 0x41: // Position x
509                                         TWDR = pos_x.i>>16;
510                                         TWI_ACK;
511                                         break;
512                                 case 0x42: // Position x
513                                         TWDR = pos_x.i>>8;
514                                         TWI_ACK;
515                                         break;
516                                 case 0x43: // Position x LSB
517                                         TWDR = pos_x.i;
518                                         TWI_ACK;
519                                         break;
520                                 case 0x44: // Position y MSB
521                                         TWDR = pos_y.i>>24;
522                                         TWI_ACK;
523                                         break;
524                                 case 0x45: // Position y
525                                         TWDR = pos_y.i>>16;
526                                         TWI_ACK;
527                                         break;
528                                 case 0x46: // Position y
529                                         TWDR = pos_y.i>>8;
530                                         TWI_ACK;
531                                         break;
532                                 case 0x47: // Position y LSB
533                                         TWDR = pos_y.i;
534                                         TWI_ACK;
535                                         break;
536                                 case 0x48: // Position angle MSB
537                                         TWDR = pos_y.i>>24;
538                                         TWI_ACK;
539                                         break;
540                                 case 0x49: // Position angle
541                                         TWDR = pos_y.i>>16;
542                                         TWI_ACK;
543                                         break;
544                                 case 0x4A: // Position angle
545                                         TWDR = pos_y.i>>8;
546                                         TWI_ACK;
547                                         break;
548                                 case 0x4B: // Position angle LSB
549                                         TWDR = pos_y.i;
550                                         TWI_ACK;
551                                         break;
552                                 case 0x94: // TLE Error status
553                                         TWDR = (PIND & 0x40)>>2 | (PINB & 0x07);
554                                         TWI_ACK;
555                                         break;
556                                 default:
557                                         TWDR = 0;
558                                         TWI_NAK;
559                         }
560                         ireg++;
561                         break;
562                 default:
563                         TWI_RESET;
564         }
565 }
566
567
568 static void update_hall1(void) {
569         unsigned char status = (PINA >> 0) & 0x3;
570         static unsigned char oldstatus=0;
571         unsigned char diff, new;
572
573         new = 0;
574         if (status & 0x1)
575                 new = 0x3;
576         if (status & 0x2)
577                 new ^= 0x1;                                     // convert gray to binary
578         diff = oldstatus - new;                         // difference last - new
579         if (diff & 0x1) {                               // bit 0 = value (1)
580                 oldstatus = new;                                        // store new as next last
581                 if (motor1_switch) pos1 += (diff & 2) - 1;              // bit 1 = direction (+/-)
582                 else pos1 -= (diff & 2) - 1;
583         }
584 }
585
586
587 static void update_hall2(void) {
588         unsigned char status = (PINA >> 4) & 0x3;
589         static unsigned char oldstatus=0;
590         unsigned char diff, new;
591
592         new = 0;
593         if (status & 0x1)
594                 new = 0x3;
595         if (status & 0x2)
596                 new ^= 0x1;                                     // convert gray to binary
597         diff = oldstatus - new;                         // difference last - new
598         if (diff & 0x1) {                               // bit 0 = value (1)
599                 oldstatus = new;                                        // store new as next last
600                 if (motor2_switch) pos2 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
601                 else pos2 += (diff & 2) - 1;
602         }
603 }
604
605
606 static void update_hall3(void) {
607         unsigned char status = (PINA >> 2) & 0x3;
608         static unsigned char oldstatus=0;
609         unsigned char diff, new;
610
611         new = 0;
612         if (status & 0x1)
613                 new = 0x3;
614         if (status & 0x2)
615                 new ^= 0x1;                                     // convert gray to binary
616         diff = oldstatus - new;                         // difference last - new
617         if (diff & 0x1) {                               // bit 0 = value (1)
618                 oldstatus = new;                                        // store new as next last
619                 if (motor3_switch) pos3 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
620                 else pos3 += (diff & 2) - 1;
621         }
622 }
623
624
625 static void update_hall4(void) {
626         unsigned char status = (PINA >> 6) & 0x3;
627         static unsigned char oldstatus=0;
628         unsigned char diff, new;
629
630         new = 0;
631         if (status & 0x1)
632                 new = 0x3;
633         if (status & 0x2)
634                 new ^= 0x1;                                     // convert gray to binary
635         diff = oldstatus - new;                         // difference last - new
636         if (diff & 0x1) {                               // bit 0 = value (1)
637                 oldstatus = new;                                        // store new as next last
638                 if (motor4_switch) pos4 += (diff & 2) - 1;              // bit 1 = direction (+/-)
639                 else pos4 -= (diff & 2) - 1;
640         }
641 }
642
643
644 static void update_motor(void) {
645         static int16_t m1_old=SHRT_MIN;
646         static int16_t m2_old=SHRT_MIN;
647         static int16_t m3_old=SHRT_MIN;
648         static int16_t m4_old=SHRT_MIN;
649
650         if (m1_old != motor1) { // update only when changed
651                 if (motor1 == 0) {
652                         // stop
653                         PORTC |= (1 << 3) | (1 << 2);
654                 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
655                         // forward
656                         PORTC &= ~(1 << 3) & ~(1 << 2);
657                 } else { // motor1 < 0
658                         // backward
659                         PORTC &= ~(1 << 2);
660                         PORTC |=  (1 << 3);
661                 }
662
663                 m1_old = motor1;
664                 OCR1A = abs(motor1);
665         }
666
667         if (m2_old != motor2) { // update only when changed
668                 if (motor2 == 0) {
669                         // stop
670                         PORTC |= (1 << 5) | (1 << 4);
671                 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
672                         // forward
673                         PORTC &= ~(1 << 5) & ~(1 << 4);
674                 } else { // motor2 < 0
675                         // backward
676                         PORTC &= ~(1 << 4);
677                         PORTC |=  (1 << 5);
678                 }
679
680                 m2_old = motor2;
681                 OCR1B = abs(motor2);
682         }
683
684         if (m3_old != motor3) { // update only when changed
685                 if (motor3 == 0) {
686                         // stop
687                         PORTC |= (1 << 7) | (1 << 6);
688                 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
689                         // forward
690                         PORTC &= ~(1 << 7) & ~(1 << 6);
691                 } else { // motor3 < 0
692                         // backward
693                         PORTC &= ~(1 << 6);
694                         PORTC |=  (1 << 7);
695                 }
696
697                 m3_old = motor3;
698                 OCR2 = abs(motor3);
699         }
700
701         if (m4_old != motor4) { // update only when changed
702                 if (motor4 == 0) {
703                         // stop
704                         PORTD |= (1 << 3) | (1 << 2);
705                 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
706                         // forward
707                         PORTD &= ~(1 << 3) & ~(1 << 2);
708                 } else { // motor4 < 0
709                         // backward
710                         PORTD &= ~(1 << 2);
711                         PORTD |=  (1 << 3);
712                 }
713
714                 m4_old = motor4;
715                 OCR0 = abs(motor4);
716         }
717 }
718
719
720 static void update_pos(void) {
721         static int16_t pos1_last=0;
722         static int16_t pos2_last=0;
723         static int16_t pos3_last=0;
724         static int16_t pos4_last=0;
725         int16_t pos1_diff; // steps
726         int16_t pos2_diff;
727         int16_t pos3_diff;
728         int16_t pos4_diff;
729         float diff_left_m, diff_right_m, angle_diff, translation;
730
731         //cli();
732         pos1_diff = pos1 - pos1_last;
733         pos2_diff = pos2 - pos2_last;
734         pos3_diff = pos3 - pos3_last;
735         pos4_diff = pos4 - pos4_last;
736         speed1 = pos1_diff/PID_T;
737         speed2 = pos2_diff/PID_T;
738         speed3 = pos3_diff/PID_T;
739         speed4 = pos4_diff/PID_T;
740         //sei();
741
742         diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M);
743         diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M);
744         angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
745
746         //cli();
747         angle.f+=angle_diff;
748         if (angle.f > 2*M_PI) angle.f-=2*M_PI;
749         else if (angle.f < 2*M_PI) angle.f+=2*M_PI;
750         //sei();
751         
752         //cli();
753         translation = (diff_left_m + diff_right_m)/2.0;
754         pos_x.f += cos(angle.f)*translation;
755         pos_y.f += sin(angle.f)*translation;
756         //sei();
757
758         pos1_last = pos1;
759         pos2_last = pos2;
760         pos3_last = pos3;
761         pos4_last = pos4;
762 }
763
764
765 static void update_pid(void) {
766         static int16_t eold1=0;
767         static int16_t eold2=0;
768         static int16_t eold3=0;
769         static int16_t eold4=0;
770         static int32_t esum1=0;
771         static int32_t esum2=0;
772         static int32_t esum3=0;
773         static int32_t esum4=0;
774
775         if (motor1_mode == MOTOR_PID) {
776                 if (speed1_wish == 0) {
777                         motor1 = 0;
778                 } else {
779                         int16_t e = speed1_wish - speed1;
780                         esum1+=e;
781                         motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
782                         eold1 = e;
783
784                         if (motor1 > 255) motor1 = 255;
785                         else if (motor1 < -255) motor1 = -255;
786                 }
787         }
788         if (motor2_mode == MOTOR_PID) {
789                 if (speed2_wish == 0) {
790                         motor2 = 0;
791                 } else {
792                         int16_t e = speed2_wish - speed2;
793                         esum2+=e;
794                         motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
795                         eold2 = e;
796
797                         if (motor2 > 255) motor2 = 255;
798                         else if (motor2 < -255) motor2 = -255;
799                 }
800         }
801         if (motor3_mode == MOTOR_PID) {
802                 if (speed3_wish == 0) {
803                         motor3 = 0;
804                 } else {
805                         int16_t e = speed3_wish - speed3;
806                         esum3+=e;
807                         motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
808                         eold3 = e;
809
810                         if (motor3 > 255) motor3 = 255;
811                         else if (motor3 < -255) motor3 = -255;
812                 }
813         }
814         if (motor4_mode == MOTOR_PID) {
815                 if (speed4_wish == 0) {
816                         motor4 = 0;
817                 } else {
818                         int16_t e = speed4_wish - speed4;
819                         esum4+=e;
820                         motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
821                         eold4 = e;
822
823                         if (motor4 > 255) motor4 = 255;
824                         else if (motor4 < -255) motor4 = -255;
825                 }
826         }
827 }
828
829
830 ISR(TIMER1_OVF_vect) {
831         update_hall1();
832         update_hall2();
833         update_hall3();
834         update_hall4();
835         
836         run_update++;
837 }
838
839
840 int main(void) {
841         // Outputs
842         DDRB = (1 << 3);
843         DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
844         DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
845
846         bootloader = 0x00;
847         setup_uart(9600);
848         uart_setup_stdout();
849
850         // I2C
851         TWAR = 0x50;
852         TWI_RESET;
853
854         // Motor 1 & 2
855         // Timer 1: Fast PWM inverting mode, Top=256 => 15.625kHz
856         // Prescaler=1
857         TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
858         TCCR1B = (1 << WGM12) | (1 << CS10);
859         OCR1A = 0;
860         OCR1B = 0;
861
862         // Motor 3
863         // Timer 2: Fast PWM inverting mode, Top=256 => 15.625kHz
864         // Prescaler=1
865         TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
866         OCR2 = 0;
867
868         // Motor 4
869         // Timer 0: Fast PWM inverting mode, Top=256 => 15.625kHz
870         // Prescaler=1
871         TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
872         OCR0 = 0;
873
874         printf("\r\nStart\r\n");
875
876         set_sleep_mode(SLEEP_MODE_IDLE);
877         // Enable Timer 1 Overflow Interrupt
878         TIMSK = (1 << TOIE1);
879         sei();
880
881         while(1) {
882                 switch(ireg) {
883                         case 0xff: // Magic reg that starts the bootloader
884                                 if (bootloader == 0xa5) {
885                                         cli();
886                                         {
887                                                 void (*start)(void) = (void*)0x1800;
888                                                 start();
889                                         }
890                                 }
891                                 break;
892                 }
893                 
894
895                 if (run_update >= 156) { // ~100Hz
896                         run_update=0;
897
898                         update_pos();
899                         update_pid();
900                         update_motor();
901                 }
902
903                 sleep_mode();
904         }
905
906         return 0;
907 }