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