Программируем робота-футболиста на Arduino
Управление моторами
Процедура:
// Управление мощностью двигателя указанного колеса // Работает с библиотекой AFMotor // Первый параметр - мотор, второй - мощность от -255 до 255 void powerMotor(AF_DCMotor motor, int value) { // выставляем направление двигателя if(value < 0) motor.run(BACKWARD); else motor.run(FORWARD); if(value == 0) motor.run(RELEASE); // выставляем мощность двигателя чтобы не превышала 255 motor.setSpeed(min(255,abs(value))); }
Пример 1:
powerMotor(motor1,-150); //будет вращать мотор1 назад с мощностью 150
Пример 2:
powerMotor(motor3,210); //будет вращать мотор3 вперед с мощностью 210
Пример 3:
powerMotor(motor2,0); //остановит мотор2
MotorShieldv2
Про сдвиговый регистр 74HC595N в составе motorshield http://www.rootadmin.com/articles/248/WebControls/WebControls/
Плата freeduino M-shield V3 - это клон платы расширения Adafruit Motor/Stepper/Servo Shield. На этой плате для формирования сигналов DIR используется сдвиговый регистр 74HCT595N (схема) он занимает выходы Arduino - D4, D7, D8 и D12:
Digital 12 DIR_LATCH Digital 4 DIR_CLK Digital 7 DIR_EN Digital 8 DIR_SER
Пример простой библиотеки для моторшилда здесь https://sites.google.com/site/analogmc/
М-шилд занимает следующие пины на ардуино: D11: DC Motor #1 / Stepper #1 (activation/speed control) D03: DC Motor #2 / Stepper #1 (activation/speed control) D05: DC Motor #3 / Stepper #2 (activation/speed control) D06: DC Motor #4 / Stepper #2 (activation/speed control)
D04, D07, D08 and D12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch
D09: Servo #1 control D10: Servo #2 control
Свободными остаются пины D2, D13 и все 6 аналоговых входов A0-A5, которые можно использовать как цифровые входы/выходы. К этим пинам можно подключать цифровые датчики.
Подробнее в брошюре Adafruit http://web.csulb.edu/~hill/ee444/Lectures/05_Adafruit%20Part%201%20SPI.pdf
/* Send data located in 8-bit variable latch_state to the 74HC595 on the Motor Shield. */ void AFMotorController::latch_tx(void) { uint8_t i; //LATCH_PORT &= ~_BV(LATCH); // - Output register clock low digitalWrite(MOTORLATCH, LOW); //SER_PORT &= ~_BV(SER); // - Serial data bit = 0 digitalWrite(MOTORDATA, LOW); for (i=0; i<8; i++) { //CLK_PORT &= ~_BV(CLK); digitalWrite(MOTORCLK, LOW); // - Shift out 8-bits // - Shift clock low if (latch_state & _BV(7-i)) { //- Is current bit of //SER_PORT |= _BV(SER); latch_state == 1 digitalWrite(MOTORDATA, HIGH); //-Yes, serial data bit = 1
} else { //SER_PORT &= ~_BV(SER); digitalWrite(MOTORDATA, LOW); //-No, serial data bit = 0 } //CLK_PORT |= _BV(CLK); digitalWrite(MOTORCLK, HIGH); //- Shift clock high, rising edge } //shift bit into shift register //LATCH_PORT |= _BV(LATCH); digitalWrite(MOTORLATCH, HIGH); // - Output register clock high, rising } //edge sends the stored bits to the //output register.