PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Motoren per Bluetooth ansteuern



Anonym
27.01.2016, 16:56
Hey Leute,

Ich versuche Seit einiger Zeit zwei Getriebemotoren über einen Arduino mit Motorshield und einem Sparkfun Bluetooth Mate Gold anzusteuern.
Ich weiß nicht was ich alles Bei meinem Code falsch gemacht habe und ob dieser überhaupt funktionieren könnte.

Hier erstmal der Code den ich auf den Arduino hochgeladen habe:

#include <SoftwareSerial.h>

char val;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

int bluetoothTx = 3;
int bluetoothRx = 4;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {

Serial.begin(9600); // start serial communication at 9600bps
}

void loop() {

if ( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}

if ( val == 'S' )

{
digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);


digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
}
delay(100);

if (val == 'W') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);


digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);

Serial.println("Vorwaerts 100%.");
}


delay(100);
if (val == 'S') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);

Serial.println("Rueckwaerts 100%.");

}
delay(100);
if (val == 'A') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);

Serial.println("Nach links drehen");

}
delay(100);
if (val == 'D') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
Serial.println("Nach rechts drehen");

}
}



Und jetzt der Processing Code:


import processing.serial.*;

Serial port;

color currentcolor;
RectButton rect1;
boolean locked = false;
void setup() {

//set up window
size(500, 500);
color baseColor = color(102, 102, 102);
currentcolor = baseColor;

// List all the available serial ports in the output pane.
// You will need to choose the port that the Wiring board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());

// Open the port that the Wiring board is connected to (in this case 1
// which is the second open port in the array)
// Make sure to open the port at the same speed Wiring is using (9600bps)
port = new Serial(this, Serial.list()[2], 9600);

// Define and create rectangle button #1
int x = 100;
int y = 80;
int size = 80;
color buttoncolor = color(153, 102, 102);
color highlight = color(102, 51, 51);
rect1 = new RectButton(x, y, size, buttoncolor, highlight);

}

void draw() {

background(currentcolor);
stroke(255);
update(mouseX, mouseY);
rect1.display();
}

void update(int x, int y) {

if (locked == false) {

rect1.update();
} else {
locked = false;
}

//Turn LED on and off if buttons pressed where
//H = on (high) and L = off (low)
if (mousePressed) {
if (rect1.pressed()) { //ON button
currentcolor = rect1.basecolor;
port.write('W');
}
}
}

class Button {

int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;

void update()
{
if (over()) {
currentcolor = highlightcolor;
} else {
currentcolor = basecolor;
}
}

boolean pressed()
{
if (over) {
locked = true;
return true;
} else {
locked = false;
return false;
}
}

boolean over()
{
return true;
}

void display()
{
}
}

class RectButton extends Button {

RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}

boolean over()
{
if ( overRect(x, y, size, size) ) {
over = true;
return true;
} else {
over = false;
return false;
}
}

void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}

boolean overRect(int x, int y, int width, int height) {

if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}


Immer wenn ich den Arduino an Strom anschließe fängt der eine Motor an zu laufen. Mein PC verbindet sich erfolgreich mit dem Arduino, aber das Programm kann nichts bewirken.

Schon mal Danke im Voraus
ToastCrafterHD

m.a.r.v.i.n
27.01.2016, 21:37
Zunächst mal würde ich empfehlen im Arduino Sketch in der setup() Funktion alle Motor Pins auf Ausgang zu schalten:


pinMode(pwmA, OUTPUT);
...

Anonym
28.01.2016, 11:35
Ich habe jetzt die Sketch stelle so abgeändert:



void setup() {
pinMode(dirA, OUTPUT);
pinMode(brakeA, OUTPUT);

pinMode(dirB, OUTPUT);
pinMode(brakeB, OUTPUT);

Serial.begin(9600); // start serial communication at 9600bps
}


Wenn ich mich mit dem Bluetooth Modul verbinde, und den Processingcode starte, schaltet die LED an dem Bluetooth-Modul von rot auf grün. Bei drücken des Knopfes geschieht aber garnichts.

ToastCrafterHD

morob
28.01.2016, 13:12
port = new Serial(this, Serial.list()[2], 9600);

gib mal den bluetooth com port direkt an, com1 ....

Anonym
28.01.2016, 14:32
Selbst wenn ich den COM Port direkt angebe komme ich zum gleichen Ergebnis wie letztes mal. Ich befürchte, des es daran liegt das sich die beiden Ports überschneide, als der pwmA Port des Bluetooth Modules und der des Motors. Denn wenn ich den Arduino an Strom anschliesse fängt der eine Motor von alleine an zu laufen.


const int pwmA = 3;


int bluetoothTx = 3;

ToastCrafterHD

m.a.r.v.i.n
28.01.2016, 19:54
Gleicher Pin für Bluetooth und Motor Treiber geht nicht. Das musst du umverkabeln zum Beispiel auf Pin 2 und 4.
Nach der Doku zum Bluetooth Mate läuft das Modul per Default mit 115,2 kBaud, nicht 9600 Baud. Oder hast du die Baudrate des Modul umgestellt?
Die Bluetooth Verbindung kann man mit diesem Sketch (https://www.arduino.cc/en/Tutorial/SoftwareSerialExample) und 2 offenen Terminal Verbindungen (USB und Bluetooth) testen (Pins und Baudraten entsprechend anpassen).

Anonym
28.01.2016, 21:15
Ja, ich habe die Baud-Rate umgestellt aber auch bei 115200 Baud ändert sich nichts, auch bei der Umänderung der Pins gibt es keine Veränderung. Die Bluetooth Verbindung funktioniert einwandfrei.

ToastCrafterHD

m.a.r.v.i.n
28.01.2016, 22:25
In deinem Arduino Sketch fehlt noch die Initialisierung der Bluetooth Schnittstelle. Und gelesen wird auch nur von der USB Schnittstelle. Über USB müsste dein Programm funktionieren.

Anonym
29.01.2016, 09:28
Über USB funktioniert mein Programme gut, muss ich es für Bluetooth wie folgt abändern ?


#include <SoftwareSerial.h>

char val;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

int bluetoothTx = 2;
int bluetoothRx = 4;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
pinMode(dirA, OUTPUT);
pinMode(brakeA, OUTPUT);

pinMode(dirB, OUTPUT);
pinMode(brakeB, OUTPUT);

Serial.begin(115200);
}

void loop() {
{
if (bluetooth.available())
{
Serial.print((char)bluetooth.read());
}
if (Serial.available())
{
bluetooth.print((char)Serial.read());
}
}

if ( val == 'S' )

{
digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);


digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
}
delay(100);

if (val == 'W') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);


digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);

Serial.println("Vorwaerts 100%.");
}


delay(100);
if (val == 'S') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);

Serial.println("Rueckwaerts 100%.");

}
delay(100);
if (val == 'A') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);

Serial.println("Nach links drehen");

}
delay(100);
if (val == 'D') {

digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);

digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
Serial.println("Nach rechts drehen");

}
if (val == '0') {

digitalWrite(pwmA, 0);
digitalWrite(pwmB, 0);

Serial.println("Motoren aus");

}
}

m.a.r.v.i.n
29.01.2016, 20:19
Es fehlt die Initialisierung der Bluetooth Softserial Schnittstelle. Ebenso stimmt die Abfrage in der loop() Funktion nicht:


...
Serial.begin(115200);
bluetooth.begin(9600);
}

void loop() {
{
if (bluetooth.available())
{
val = bluetooth.read();
Serial.print(val);
}
else if (Serial.available())
{
val = Serial.read();
bluetooth.print(val);
}
...

Anonym
29.01.2016, 22:19
Danke soweit funktioniert es super

ToastCrafterHD

Anonym
07.02.2016, 19:48
Da mein erstes Shield meine starken Motoren nicht auf Dauer aushält, habe ich mir jetzt das Pololu Dual VNH5019 Motor Driver Shield for Arduino
gekauft (https://www.pololu.com/product/2507). Diese funktioniert super und schaft die Motore mit links aber ich kann es nur über USB steuern und nicht über Bluetooth. Wo liegt mein Fehler?

Mein Arduino Code:

#include <DualVNH5019MotorShield.h>
#include <SoftwareSerial.h>

char val;
int i = 100;
int o = -100;
int p = 0;

int bluetoothTx = 2;
int bluetoothRx = 4;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

DualVNH5019MotorShield md;

void setup()
{
md.init();
Serial.begin(115200);
bluetooth.begin(115200);

}

void loop()
{
{
if (bluetooth.available())
{
val = bluetooth.read();
Serial.print(val);
}
else if (Serial.available())
{
val = Serial.read();
bluetooth.print(val);
}
}
// anfang motor steuerung
if (val == 'W') {
md.setM1Speed(i);
md.setM2Speed(i);
}

if (val == '0') {
md.setM1Speed(p);
md.setM2Speed(p);
}

if (val == 'S') {
md.setM1Speed(o);
md.setM2Speed(o);
}
if (val == 'A') {
md.setM1Speed(i);
md.setM2Speed(o);
}
if (val == 'D') {
md.setM1Speed(o);
md.setM2Speed(i);
}
}

und mein Processing Code:

import processing.serial.*;

Serial port;
int val;

void setup()
{
size(200, 200);
port = new Serial(this, "COM7", 115200);
}

void draw() {

if (keyPressed) {
if(key == CODED){
if (keyCode == UP) {
port.write('W');
}
if (keyCode == DOWN) {
port.write('S');
}
if (keyCode == RIGHT) {
port.write('D');
}
if (keyCode == LEFT) {
port.write('A');
}
}
if (key == '0') {
port.write('0');
}
}
}

Danke schon mal
ToastCrafterHD

m.a.r.v.i.n
07.02.2016, 21:42
Das Dual VNH5019 Motor Driver Shield verwendet Pin 2 und 4. Das überschneidet sich mit den Bluetooth Pins. Pin 3 und 5 wären noch frei.