Robofun 機器人論壇
標題:
如何用 wifi shield 做自走車的無線控制?
[打印本頁]
作者:
qqq12414
時間:
2014-7-9 01:36
標題:
如何用 wifi shield 做自走車的無線控制?
本帖最後由 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();
}
}
}
作者:
vegewell
時間:
2014-7-11 03:28
回復
1#
qqq12414
>>從網路上找到的範例修改後
你意思是說,沒有經過修改的範例,自走車不須接上電腦USB線,在手機端的廣播也有作用,拔掉傳輸線一樣可以控制, 是嗎?
作者:
qqq12414
時間:
2014-7-11 21:28
本帖最後由 qqq12414 於 2014-7-11 21:31 編輯
回復
2#
vegewell
找到問題了@@程式部分沒問題,後來發現是POWER不夠,所以拔掉USB線馬達會動不了
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2