Last active
January 25, 2017 05:35
-
-
Save takehaya/61b21d5464f9b193a32423ac1c4d0f2a 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 by Takeru Hayasaka to 11/9~1/25 | |
All rights reserved. | |
/* Create a WiFi access point and provide a web server on it. */ | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <Servo.h> | |
#include <Wire.h> | |
#include "WifiControll.h" | |
Servo myservo; | |
int g_iMotor = 0; | |
int g_iIncrease = 10; | |
WiFiUDP udp; | |
int write_vset(byte,byte); | |
void setup() { | |
Serialbegin(115200);//Serial | |
Wire.begin(DC_MOTER_IN1_SDA, DC_MOTER_IN1_SCL); | |
WiFi.config(ip, WiFi.gatewayIP(), WiFi.subnetMask()); // setting IP Address | |
WiFi.softAP(ssid, pass);//Wifi | |
IPAddress myIP = WiFi.softAPIP(); | |
Serialprint("AP IP address: "); Serialprintln(myIP); | |
Serialprint("Starting UDP"); | |
udp.begin(localPort); | |
Serialprint("Local port: "); Serialprintln(udp.localPort()); | |
// ESP8266のanalogWrite()の引数の範囲の初期値は[0-1023]。 | |
// 通常のArduinoのanalogWrite()の引数の範囲と同じ[0-255]に変更する。 | |
// analogWriteRange(255); | |
//analogWriteRange(128); | |
// pinMode(DC_MOTER_IN1_1, OUTPUT); | |
// pinMode(DC_MOTER_IN1_2, OUTPUT); | |
pinMode(LED_Light, OUTPUT); | |
myservo.attach(SERVO_IN); | |
delay(50); | |
myservo.write(90); | |
write_vset(MIN_VSET, M_BRAKE); | |
// analogWrite(DC_MOTER_IN1_1, 0); | |
// analogWrite(DC_MOTER_IN1_2, 0); | |
} | |
void SarvoDrive(int degrees, bool st)//(角度、正逆) | |
{ | |
int pos = 0; | |
if (st) { | |
for (pos = 0; pos <= degrees; pos += 1) // goes from 0 degrees to degrees | |
{ // in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(5); // waits 15ms for the servo to reach the position | |
} | |
} else { | |
for (pos = degrees; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees | |
{ | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(5); // waits 15ms for the servo to reach the position | |
} | |
} | |
} | |
void loop() { | |
//signalData:L,M,S<=>LED点灯・Moter駆動・ステアリング駆動 | |
//ActionParameter:Type of Integer | |
//データフォーマット[signalData ActionParameter] | |
int rlen, NoData = 0;//データの長さ、データのこない分のカウンターバッファ | |
int Val_L = 0, Val_M = 0, Val_S = 0;//LED点灯・Moter駆動・ステアリング駆動 | |
while (true) { | |
//データが来ないので待つ | |
if (!(rlen = udp.parsePacket())) { | |
if (++NoData > 50) { | |
} | |
delay(10); | |
continue; | |
} | |
NoData = 0;//データがきたので来なかった記録をリセット | |
udp.read(packetBuffer, (rlen > OSC_PACKET_SIZE) ? OSC_PACKET_SIZE : rlen);//データを読み出す。 | |
Serialprint("L= "); Serialprintln(packetBuffer); | |
//指定の先頭文字でチェック | |
if (strncmp(&packetBuffer[0], "/osc/", 5) == 0) { | |
Serialprintln(packetBuffer[5]); | |
//指定のsignalDataでチェック | |
switch (packetBuffer[5]) { | |
case 'L'://LED | |
Serialprintln("signalDataでチェック"); | |
Serialprintln(packetBuffer[6]); | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_L = atoi(packetBuffer); | |
Serialprintln(Val_L); | |
analogWrite(LED_Light, Val_L); | |
break; | |
case 'S'://ステアリング駆動 | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_S = atoi(packetBuffer); | |
if (Val_S >= (45 - 4) && Val_S <= (45 + 4)) { | |
Val_S = 45; | |
} | |
if (Val_S >= 45) { | |
myservo.write(Val_S); | |
} else { | |
myservo.write(Val_S); | |
} | |
break; | |
case 'M'://モーター駆動 | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_M = atoi(packetBuffer); | |
//ブレーキ==停止 | |
if (Val_M >= (64 - 6) && Val_M <= (64 + 6)) { | |
write_vset(MIN_VSET, M_BRAKE); | |
}else{ | |
//順方向制御 | |
if (Val_M >= 64) { | |
for (byte i = MIN_VSET; i <= MAX_VSET; i++) { | |
write_vset(i, M_NORMAL); | |
delay(12); | |
Serialprint("+++++"); Serialprint(Val_M); | |
} | |
} | |
//逆方向制御 | |
else { | |
// 逆方向 徐々にスピードを上げる | |
for (byte i = MIN_VSET; i <= MAX_VSET; i++) { | |
write_vset(i, M_REVERSE); | |
delay(12); | |
} | |
Serialprint("------"); Serialprint(Val_M); | |
} | |
} | |
break; | |
} | |
Serialprint("L= "); Serialprint(Val_L); | |
Serialprint("M= "); Serialprint(Val_M); | |
Serialprint("S="); Serialprintln(Val_S); | |
} | |
delay(10); | |
} | |
} | |
// 制御コマンド送信 | |
int write_vset(byte vs, byte ctr) { | |
Wire.beginTransmission(DRV_ADR); | |
Wire.write(CTR_ADR); | |
Wire.write( ctr + (vs << 2) ); | |
return Wire.endTransmission(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment