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