Android-управляемая робот-платформа на Raspberry Pi и Motor Shield
Компоненты и материалы
| Android-устройство (телефон/планшет) | 1 |
| Arduino 101 | 1 |
| Raspberry Pi 2 Model B | 1 |
| Motor Shield (DK Electronic) | 1 |
| Официальный Wi-Fi донгл для Raspberry Pi | 1 |
| Power Bank Trust 5V | 1 |
Приложения и онлайн-сервисы
- Microsoft Windows 10 IoT Core
- Arduino IDE
- Microsoft Visual Studio 2015
О проекте
В итоге получим наземную робот-платформу, управляемую акселерометром Android-устройства.
В проекте используются: Motor Shield, Raspberry Pi, Arduino, DC-двигатели.
ШАГ 1: I2C между Raspberry Pi и Arduino
Сначала настраиваем I2C-шину между RPi и Arduino. Почему I2C — тут.
Код для Arduino (I2C Slave + Motor Shield):
#include
#define MyAddress 0x40
#include
AF_DCMotor motorhiz(3); // скорость
AF_DCMotor motoryon(4); // направление
byte DataToBeSend[1];
byte ReceivedData;
int counter = 0;
void setup() {
Serial.begin(9600);
Wire.begin(MyAddress);
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
motorhiz.setSpeed(254); motorhiz.run(RELEASE);
motoryon.setSpeed(254); motoryon.run(RELEASE);
}
void loop() { /* пустой, работа в прерываниях I2C */ }
void I2CReceived(int NumberOfBytes) {
ReceivedData = Wire.read();
int ReceivedDataInt = (int)ReceivedData;
// Протокол: X-данные (>=100) — скорость, Y-данные (<100) — поворот
// Диапазоны: 100-103 стоп, 105-154 вперёд, 155-254 назад (скорость = (val-155)*10+50)
// Y: 0-3 стоп, 5-54 вперёд, 55-... назад
// Подробная логика в коде ниже...
}
void I2CRequest() { /* опционально */ }

I2C: SDA (Arduino A4) — SDA (RPi GPIO2), SCL (Arduino A5) — SCL (RPi GPIO3).
GND — GND. Arduino питается от 5V RPi (Vout), внешний блок питания не нужен.
ШАГ 2: Код для Raspberry Pi (веб-сервер на Windows IoT Core)
RPi работает как веб-сервер, принимает данные от Android-приложения и пересылает их Arduino по I2C. Код в разделе Code (Windows IOT Web Server).
ШАГ 3: Android-приложение (Visual Studio Cordova)
Приложение читает акселерометр и отправляет данные на веб-сервер RPi через AJAX. Код в разделе Code (Android Remote Controller).
КРАТКО: АРХИТЕКТУРА
- Android-приложение → акселерометр → AJAX → веб-сервер на Raspberry Pi (Windows IoT Core).
- Raspberry Pi → I2C → Arduino.
- Arduino → Motor Shield → DC-двигатели.
Код
Wiri.h — I2C и Motor Shield (Arduino)
#include
#define MyAddress 0x40
#include
AF_DCMotor motorhiz(3);
AF_DCMotor motoryon(4);
byte DataToBeSend[1];
byte ReceivedData;
int counter = 0;
void setup() {
Serial.begin(9600);
Wire.begin(MyAddress);
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
motorhiz.setSpeed(254); motorhiz.run(RELEASE);
motoryon.setSpeed(254); motoryon.run(RELEASE);
}
void loop() { }
void I2CReceived(int NumberOfBytes) {
ReceivedData = Wire.read();
int ReceivedDataInt = (int)ReceivedData;
String ReceivedDataStr = String(ReceivedData);
Serial.println(ReceivedDataInt);
if(ReceivedDataInt >= 100) { // X-axis: скорость
if(ReceivedDataInt > 145 && ReceivedDataInt < 154) { motorhiz.run(RELEASE); }
else if(ReceivedDataInt >= 100 && ReceivedDataInt < 104) { motorhiz.run(RELEASE); }
else if(ReceivedDataInt >= 155) { // назад
ReceivedDataInt -= 155;
int motorSpeed = (ReceivedDataInt * 10) + 50;
if(motorSpeed > 254) motorSpeed = 254;
motorhiz.setSpeed(motorSpeed); motorhiz.run(BACKWARD);
}
else if(ReceivedDataInt >= 105) { // вперёд
ReceivedDataInt -= 105;
int motorSpeed = (ReceivedDataInt * 10) + 50;
if(motorSpeed > 254) motorSpeed = 254;
motorhiz.setSpeed(motorSpeed); motorhiz.run(FORWARD);
}
} else { // Y-axis: поворот
if(ReceivedDataInt > 45 && ReceivedDataInt < 54) { motoryon.run(RELEASE); }
else if(ReceivedDataInt >= 0 && ReceivedDataInt < 4) { motoryon.run(RELEASE); }
else if(ReceivedDataInt >= 55) { // назад
ReceivedDataInt -= 55;
int motorSpeed = (ReceivedDataInt * 12) + 50;
if(motorSpeed > 254) motorSpeed = 254;
motoryon.setSpeed(motorSpeed); motoryon.run(BACKWARD);
}
else if(ReceivedDataInt >= 5) { // вперёд
ReceivedDataInt -= 5;
int motorSpeed = (ReceivedDataInt * 12) + 50;
if(motorSpeed > 254) motorSpeed = 254;
motoryon.setSpeed(motorSpeed); motoryon.run(FORWARD);
}
}
}
void I2CRequest() { /* не используется */ }
Android Remote Controller (C#)
Код доступен для скачивания (превью отсутствует).
Windows IOT Web Server (C#)
Код доступен для скачивания (превью отсутствует).
3D-модели и схемы
Схема подключения Arduino — Raspberry Pi
Схемы (детали)
Подробнее: Hackster.io
Производственный процесс
- Управление светодиодом с PIR-датчиком движения на Raspberry Pi
- Создание умного робота с Bridge Shield — интеграция Raspberry Pi и Arduino
- Создание Bluetooth-робота на Raspberry Pi с аудио-обратной связью
- Создание интернет-управляемого видеostreaming-робота на Arduino и Raspberry Pi
- Жестово-управляемый робот на Raspberry Pi
- Создание WiFi-робота на Raspberry Pi и Android-смартфоне
- Построение Wi-Fi-управляемого робота на Raspberry Pi с Python — пошаговое руководство
- Робот для поиска людей на базе Arduino Uno: пошаговая интеграция датчиков
- Сборка Bluetooth-робота-шпиона на Arduino с управлением с телефона
- Универсальный Android-пульт на Arduino, 1Sheeld и ИК-управлении