29 fInitialByte(initialByte)
48 fAddress = (addr & 0x7);
63 fPort->write(&fInitialByte, 1);
87 int turn = map(fTurnPower, -127, 127, -255, 255);
88 int throttle = map(power, -127, 127, -255, 255);
89 int left = throttle +
turn;
90 int right = throttle -
turn;
91 float scaleLeft = abs(left/255.0);
92 float scaleRight = abs(right/255.0);
93 float scaleMax = max(scaleLeft, scaleRight);
94 scaleMax = max(1.0f, scaleMax);
95 left = int(constrain(left/scaleMax, -255, 255));
96 right = int(constrain(right/scaleMax, -255, 255));
97 motor(map(left, -255, 255, -127, 127),
98 map(right, -255, 255, -127, 127));
103 const uint8_t headerByte = 0x85;
106 uint8_t addressByte = fAddress;
107 uint8_t commandByte = map(power, -127, 127, 0, 255);
108 uint8_t checksum = headerByte + addressByte + commandByte;
109 fPort->write(&headerByte, 1);
110 fPort->write(&addressByte, 1);
111 fPort->write(&commandByte, 1);
112 fPort->write(&checksum, 1);
119 void motor(
int powerLeft,
int powerRight)
121 printf(
"MOTOR{%d}:%d:%d\n",
address(), powerLeft, powerRight);
122 const uint8_t headerByte = 0x85;
125 uint8_t addressByte = fAddress;
126 uint8_t commandByte = map(powerLeft, -127, 127, 0, 255);
127 uint8_t checksum = headerByte + addressByte + commandByte;
128 printf(
" LEFT: address=%02X cmd=%02X checksum=%02X\n", addressByte, commandByte, checksum);
129 fPort->write(&headerByte, 1);
130 fPort->write(&addressByte, 1);
131 fPort->write(&commandByte, 1);
132 fPort->write(&checksum, 1);
135 addressByte = (fAddress | 0x40);
136 commandByte = map(powerRight, -127, 127, 0, 255);
137 checksum = headerByte + addressByte + commandByte;
138 printf(
" RIGHT: address=%02X cmd=%02X checksum=%02X\n", addressByte, commandByte, checksum);
139 fPort->write(&headerByte, 1);
140 fPort->write(&addressByte, 1);
141 fPort->write(&commandByte, 1);
142 fPort->write(&checksum, 1);
165 uint8_t fInitialByte;