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