|
本帖最後由 qqq12414 於 2014-7-9 01:38 編輯
最近剛入門 有許多問題還不太瞭解
手邊只有這塊wifi版:http://www.seeedstudio.com/depot/Wifi-Shield-p-1220.html
目前是接上飆機器人的ABB_CAR 想做無線控制
從網路上找到的範例修改後,我讓自走車連上手機的基地台後,用UDP廣播控制馬達動作
可是現在有一個問題,自走車必須接上電腦USB線,在手機端的廣播才有作用,一旦拔掉傳輸線就沒辦法控制
以下是我的程式碼,請問我的問題在哪邊呢?
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "WiFly.h"
#include "Servo.h"
#define SSID "AndroidAP"
#define KEY ""
#define AUTH WIFLY_AUTH_OPEN
#define UDP_HOST_IP "192.168.43.44" // broadcast
#define UDP_REMOTE_PORT 8080
#define UDP_LOCAL_PORT 5555
// Pins' connection
// Arduino WiFly
// 2 <----> TX
// 3 <----> RX
SoftwareSerial uart(2, 3);
WiFly wifly(uart);
int deg=90;
Servo servoLeft;
Servo servoRight;
Servo servoCamera;
void setupUDP(const char *host_ip, uint16_t remote_port, uint16_t local_port)
{
char cmd[32];
wifly.sendCommand("set w j 1\r", "AOK"); // enable auto join
wifly.sendCommand("set i p 1\r", "AOK");
snprintf(cmd, sizeof(cmd), "set i h %s\r", host_ip);
wifly.sendCommand(cmd, "AOK");
snprintf(cmd, sizeof(cmd), "set i r %d\r", remote_port);
wifly.sendCommand(cmd, "AOK");
snprintf(cmd, sizeof(cmd), "set i l %d\r", local_port);
wifly.sendCommand(cmd, "AOK");
wifly.sendCommand("save\r");
wifly.sendCommand("reboot\r");
}
void forward(int time){
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1300);
delay(time);
}
void backward(int time){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1700);
delay(time);
}
void turnLeft(int time){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1300);
delay(time);
}
void turnRight(int time){
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(time);
}
void bestop(int time){
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
delay(time);
}
void camup(){
if(deg == 180){
servoCamera.write(deg);
}
else {
deg += 45;
servoCamera.write(deg);
}
}
void camdown(){
if ( deg == 0){
servoCamera.write(deg);
}
else {
deg-=45;
servoCamera.write(deg);
}
}
void setup() {
Serial.begin(9600);
servoLeft.attach(13);
servoRight.attach(12);
servoCamera.attach(10);
servoCamera.write(deg);
Serial.println("--------- WIFLY UDP --------");
uart.begin(9600); // WiFly UART Baud Rate: 9600
wifly.reset();
while (1) {
Serial.println("Try to join " SSID );
if (wifly.join(SSID, KEY, AUTH)) {
Serial.println("Succeed to join " SSID);
wifly.clear();
break;
} else {
Serial.println("Failed to join " SSID);
Serial.println("Wait 1 second and try again...");
delay(1000);
}
}
setupUDP(UDP_HOST_IP, UDP_REMOTE_PORT, UDP_REMOTE_PORT);
wifly.sendCommand("set sys autoconn 1");
delay(1000);
wifly.clear();
Serial.println("Succeed to create UDP");
}
unsigned int time_point = 0;
char receivedata;
void loop() {
while (wifly.available()) {
receivedata = wifly.read();
Serial.print(receivedata);
if(receivedata==49 ){
forward(20);
}
else if(receivedata==50){
backward(20);
}
else if(receivedata==51){
turnLeft(200);
}
else if(receivedata==52){
turnRight(200);
}
else if(receivedata== 53){
bestop(400);
}
else if(receivedata== 54){
camup();
}
else if(receivedata== 55){
camdown();
}
}
} |
|