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