Reputation: 66
#include <Servo.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
Servo bldc;
double roll,pitch,yaw,thr;
char packet[255];
struct __attribute__((packed)) JoyStick {
double yaw;
double thr;
double pitch;
double roll;
int32_t mode;
double array[9];
};
#define WIFI_SSID "K.G.F - 2"
#define WIFI_PASS "assafkhan92786"
#define UDP_PORT 4210
//----------------------------------------
WiFiUDP UDP;
void setup() {
Serial.begin(115200); //115200
//-----------------------------
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin listening to UDP port
UDP.begin(UDP_PORT);
Serial.print("Listening on UDP port ");
Serial.println(UDP_PORT);
//----------------------------
bldc.attach(D5,1000,2000);
}
void udp_rcv()
{
int packetSize = UDP.parsePacket();
JoyStick myJoystick;
if (packetSize) {
//Serial.printf("Received %d bytes\n", packetSize);
if (UDP.read(packet, packetSize) > 0) {
memcpy(&myJoystick, packet, packetSize); //copy packet array to the a struct
//Serial.print(myJoystick.roll,HEX);
yaw=myJoystick.yaw;
thr=myJoystick.thr;
pitch=myJoystick.pitch;
roll=myJoystick.roll;
}
}
}
void loop()
{
udp_rcv();
Serial.println(thr);// the received values are in range from 0 to 1023
bldc.writeMicroseconds(thr);
}
I am trying to controll a 1000kv bldc motor(A2212) with a 30A ESC using esp8266, but I am completely lost, and I am unable to ARM the motor and controll it, although I am reciving the input from my laptop via UDP, please guide me, on how to ARM the ESC/MOTOR, and controll its speed using ESP8266 (wemos D1 mini) also I have used the d5 and d6 pin in wemos_d1_mini, so please let me know which other pins i can use.
Upvotes: 2
Views: 117
Reputation: 66
This solution works fine for me, at last i figured it out, seems it was very easy, you just need to send the min throttle wait for 2 seconds and then send the max throttle (pulse); boom ESC is calibrated now.
#include <Servo.h>
// ------------------------------------------------------------------------
// Customize here pulse lengths as needed
#define MIN_PULSE_LENGTH 1000 // Minimum pulse length in µs
#define MAX_PULSE_LENGTH 2000 // Maximum pulse length in µs
// ---------------------------------------------------------------------------
Servo mot;
char data;
// ---------------------------------------------------------------------------
/**
* Initialisation routine
*/
void setup() {
Serial.begin(9600);
mot.attach(D5, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
delay(5000);
//-------------------------------------
Serial.println("Sending minimum throttle");
mot.writeMicroseconds(MIN_PULSE_LENGTH);
delay(2500);
//-------------------------------------
Serial.println("Sending maximum throttle");
mot.writeMicroseconds(MAX_PULSE_LENGTH);
//-------------------------------------
}
void loop()
{
test();
}
void test()
{
for (int i = MIN_PULSE_LENGTH; i <= MAX_PULSE_LENGTH; i += 5) {
Serial.print("Pulse length = ");
Serial.println(i);
mot.writeMicroseconds(i);
delay(200);
}
Serial.println("STOP");
mot.writeMicroseconds(MIN_PULSE_LENGTH);
}
Upvotes: 1