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