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