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