Created
December 6, 2017 03:11
-
-
Save vixtory09678/e9618ff7d579ec9eb4667ae5ea983973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create a WiFi access point and provide a web server on it. */ | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#define buffer_size 22 | |
#define period_udp 1 | |
#define period_led 1000 | |
#define IN1 12 | |
#define IN2 1 | |
#define IN3 13 | |
#define IN4 2 | |
#define PWM_A 0 | |
#define PWM_B 15 | |
/* Set these to your desired credentials. */ | |
const String ssid = "honey_robot-cmmc"; | |
const String password = String("12345678"); | |
unsigned int localPort = 12345; | |
unsigned long time_now, time_prev_udp; | |
int cb = 1; | |
char packetBuffer[buffer_size] = {0}; //buffer to hold incoming and outgoing packets | |
WiFiUDP udp; | |
int count = 0; | |
IPAddress local_ip(192, 168, 5, 1); | |
IPAddress gateway(192, 168, 5, 1); | |
IPAddress subnet(255, 255, 255, 255); | |
void driveMotorA(int x,int y); | |
void Read_UDP(); | |
void setup() | |
{ | |
pinMode(IN2, OUTPUT); | |
pinMode(IN1, OUTPUT); | |
pinMode(IN3, OUTPUT); | |
pinMode(IN4, OUTPUT); | |
analogWriteFreq(30); | |
pinMode(PWM_A, OUTPUT); | |
pinMode(PWM_B, OUTPUT); | |
delay(3000); | |
Serial.begin(115200); | |
WiFi.disconnect(); | |
/* You can remove the password parameter if you want the AP to be open. */ | |
WiFi.softAP(ssid.c_str(), password.c_str()); | |
delay(500); | |
WiFi.softAPConfig(local_ip, gateway, subnet); | |
delay(500); | |
IPAddress myIP = WiFi.softAPIP(); | |
if ( udp.begin(localPort) > 0) { | |
//if success | |
Serial.println(F("create udp server success")); | |
} else { | |
// if not success | |
Serial.println(F("create udp server not success")); | |
} | |
Serial.println(F("ssid:kkao_server")); | |
Serial.println(F("pass:12345678")); | |
Serial.println(F("active")); | |
delay(3000); | |
time_now = millis(); | |
time_prev_udp = time_now; | |
time_prev_led = time_now; | |
} | |
void loop() | |
{ | |
time_now = millis(); | |
if (time_now - time_prev_udp >= period_udp) | |
{ | |
time_prev_udp = time_now; | |
Read_UDP(); | |
} | |
delay(0); | |
} | |
void Read_UDP() | |
{ | |
cb = udp.parsePacket(); | |
if (!cb) { | |
return; | |
} | |
memset(packetBuffer, 0, buffer_size); // clear mem | |
udp.read(packetBuffer, cb); // read the packet into the buffer | |
int x = 0,y = 0; | |
x = packetBuffer[1]; | |
y = packetBuffer[2]; | |
if(x <= 255 && x >= 156){ | |
x = x - 254; | |
} | |
if(y <= 255 && y >= 156){ | |
y = y - 254; | |
} | |
driveMotor(x,y); | |
} | |
void driveMotor(int x , int y){ | |
int speedR = (x-y)*(-1); //OLD VALUE | |
int speedL = (x+y); | |
if(speedR > 0){ | |
digitalWrite(IN1,HIGH); | |
digitalWrite(IN2,LOW); | |
}else if(speedR < 0){ | |
digitalWrite(IN1,LOW); | |
digitalWrite(IN2,HIGH); | |
} | |
if(speedL > 0){ | |
digitalWrite(IN3,HIGH); | |
digitalWrite(IN4,LOW); | |
}else if(speedL < 0){ | |
digitalWrite(IN3,LOW); | |
digitalWrite(IN4,HIGH); | |
} | |
if( (speedR) > 100){ | |
speedR = 100; | |
} | |
if(abs(speedL) > 100){ | |
speedL = 100; | |
} | |
speedR = map(speedR,0,100,0,1023); | |
speedL = map(speedL,0,100,0,1023); | |
Serial.print(" speedR : "); | |
Serial.print(speedR); | |
Serial.print(" speedL : "); | |
Serial.println(speedL); | |
analogWrite(PWM_A,abs(speedR)); | |
analogWrite(PWM_B,abs(speedL)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment