У нас в программе есть несколько функций. Одной из них должно быть сжатие захвата манипулятора с помощью Сервопривода(Модель:Towerpro mg996R). Но, к сожалению, при подключении его в программе перестают работать другие функции. Питание осуществляется от батарей(Мощностью 12 Вольт). Используется плата Arduino UNO.
Пример кода:
#define DRIVE_D0 3 //DRIVE_D0
#define DRIVE_D1 5 //DRIVE_D1
#define DRIVE_D2 6 //DRIVE_D2
#define DRIVE_D3 9 //DRIVE_D3
#define button_UP 4
#define button_LEFT 12
#include <Servo.h>
Servo s1;
int speed_motor = 80;
String a;
// variables will change:
int button_State_UP = 0;
int button_State_LEFT = 0;
void full_stop_motors()
{
// Задаём направление
digitalWrite (DRIVE_D0, 0);
digitalWrite (DRIVE_D1, 0);
digitalWrite (DRIVE_D2, 0);
digitalWrite (DRIVE_D3, 0);
}
void press_servo()
{
s1.write(60);
}
void let_off_servo()
{
s1.write(0);
}
void up()
{
// Задаём направлениеd
while (button_State_UP != 1) {
button_State_UP = digitalRead(button_UP);
analogWrite (DRIVE_D0, 0);
analogWrite (DRIVE_D1, speed_motor);
}
full_stop_motors();
}
void down()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void ydown()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void turn_left()
{
while (button_State_LEFT != 1) {
button_State_LEFT = digitalRead(button_LEFT);
analogWrite (DRIVE_D2, 0);
analogWrite (DRIVE_D3, speed_motor);
}
full_stop_motors();
}
void turn_right_c()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1300);
full_stop_motors();
}
void turn_right()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1400);
full_stop_motors();
}
void centre()
{
turn_left();
full_stop_motors();
turn_right_c();
full_stop_motors();
}
void setup()
{
pinMode(button_UP, INPUT);
pinMode(button_LEFT, INPUT);
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
pinMode(DRIVE_D0, OUTPUT);
pinMode(DRIVE_D1, OUTPUT);
pinMode(DRIVE_D2, OUTPUT);
pinMode(DRIVE_D3, OUTPUT);
full_stop_motors();
Serial.begin(9600);
}
void loop() {
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
if (Serial.available()) { //From RPi to Arduino
char r = Serial.read(); //conveting the value of chars to integer
if (r == 'r') {
turn_right();
Serial.println(r);
}
else if (r == 'l') {
turn_left();
Serial.println(r);
}
else if (r == 'y') {
ydown();
Serial.println(r);
}
else if (r == 'u') {
up();
Serial.println(r);
}
else if (r == 'd') {
down();
Serial.println(r);
}
else if (r == 's') {
full_stop_motors();
Serial.println(r);
}
else if (r == 'c') {
centre();
Serial.println(r);
}
else if (r == '*') {
press_servo();
Serial.println(r);
}
else if (r == '/') {
let_off_servo();
Serial.println(r);
}
}
}
#define DRIVE_D0 3 //DRIVE_D0
#define DRIVE_D1 5 //DRIVE_D1
#define DRIVE_D2 6 //DRIVE_D2
#define DRIVE_D3 9 //DRIVE_D3
#define button_UP 4
#define button_LEFT 12
#include <Servo.h>
Servo s1;
int speed_motor = 80;
String a;
// variables will change:
int button_State_UP = 0; // variable for reading the pushbutton status
int button_State_LEFT = 0;
void full_stop_motors()
{
// Задаём направление
digitalWrite (DRIVE_D0, 0);
digitalWrite (DRIVE_D1, 0);
digitalWrite (DRIVE_D2, 0);
digitalWrite (DRIVE_D3, 0);
}
void press_servo()
{
s1.write(60);
}
void let_off_servo()
{
s1.write(0);
}
void up()
{
// Задаём направлениеd
while (button_State_UP != 1) {
button_State_UP = digitalRead(button_UP);
analogWrite (DRIVE_D0, 0);
analogWrite (DRIVE_D1, speed_motor);
}
full_stop_motors();
}
void down()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void ydown()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void turn_left()
{
while (button_State_LEFT != 1) {
button_State_LEFT = digitalRead(button_LEFT);
analogWrite (DRIVE_D2, 0);
analogWrite (DRIVE_D3, speed_motor);
}
full_stop_motors();
}
void turn_right_c()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1300);
full_stop_motors();
}
void turn_right()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1400);
full_stop_motors();
}
void centre()
{
turn_left();
full_stop_motors();
turn_right_c();
full_stop_motors();
}
void setup()
{
pinMode(button_UP, INPUT);
pinMode(button_LEFT, INPUT);
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
pinMode(DRIVE_D0, OUTPUT);
pinMode(DRIVE_D1, OUTPUT);
pinMode(DRIVE_D2, OUTPUT);
pinMode(DRIVE_D3, OUTPUT);
full_stop_motors();
Serial.begin(9600);
}
void loop() {
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
if (Serial.available()) { //From RPi to Arduino
char r = Serial.read(); //conveting the value of chars to integer
if (r == 'r') {
turn_right();
Serial.println(r);
}
else if (r == 'l') {
turn_left();
Serial.println(r);
}
else if (r == 'y') {
ydown();
Serial.println(r);
}
else if (r == 'u') {
up();
Serial.println(r);
}
else if (r == 'd') {
down();
Serial.println(r);
}
else if (r == 's') {
full_stop_motors();
Serial.println(r);
}
else if (r == 'c') {
centre();
Serial.println(r);
}
else if (r == '*') {
press_servo();
Serial.println(r);
}
else if (r == '/') {
let_off_servo();
Serial.println(r);
}
}
}