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