You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.2 KiB
C++
145 lines
4.2 KiB
C++
#include <ArduinoOSCWiFi.h>
|
|
#include <Adafruit_MotorShield.h>
|
|
|
|
const int C_BLACKFEED = 3;
|
|
const int C_BLACKHEAD = 4;
|
|
|
|
const int recv_port = 10000;
|
|
const int send_port = 6060;
|
|
|
|
int blackHeadSpeed = 150;
|
|
int blackFeedSpeed = 150;
|
|
|
|
uint8_t stepperType = SINGLE;
|
|
int stepperSteps = 100;
|
|
int stepperRPM = 10;
|
|
|
|
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
|
|
Adafruit_DCMotor* blackHead = AFMS.getMotor(C_BLACKHEAD);
|
|
Adafruit_DCMotor* blackFeed = AFMS.getMotor(C_BLACKFEED);
|
|
|
|
// 48 = steps count based on revolution of the motor (7.5degree/step motor has 360/7.5 = 48), 2 = motor number
|
|
Adafruit_StepperMotor* stepper = AFMS.getStepper(stepperRPM, 1);
|
|
|
|
// customise as needed
|
|
const char* ssid = "XPUB";
|
|
const char* password = "5fff132d";
|
|
const char* host = "192.168.1.174";
|
|
const IPAddress ip(192, 168, 1, 171);
|
|
const IPAddress gateway(192, 168, 1, 1);
|
|
const IPAddress subnet(255, 255, 255, 0);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// WiFi Stuff
|
|
// WiFi.setMinSecurity(WIFI_AUTH_WPA_PSK); // didnt work on esp?
|
|
// WiFi.mode(WIFI_MODE_STA); // didnt work on esp?
|
|
WiFi.begin(ssid, password);
|
|
WiFi.config(ip, gateway, subnet);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
WiFi.localIP();
|
|
};
|
|
|
|
if (!AFMS.begin()) { // here for pwm
|
|
Serial.println("Could not find Motor Shield. Check wiring.");
|
|
while (1)
|
|
;
|
|
};
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected.");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
Serial.print("ESP CPU Frequency in MHz: ");
|
|
Serial.println(ESP.getCpuFreqMHz());
|
|
|
|
stepper->setSpeed(stepperRPM); // RPM = is how many revolutions per minute you want the stepper to turn.
|
|
stepper->release();
|
|
blackHead->setSpeed(blackHeadSpeed);
|
|
blackHead->run(RELEASE);
|
|
blackFeed->setSpeed(blackFeedSpeed);
|
|
blackFeed->run(RELEASE);
|
|
|
|
OscWiFi.subscribe(recv_port, "/stopall", [](const OscMessage& m) {
|
|
blackFeed->run(RELEASE);
|
|
blackHead->run(RELEASE);
|
|
stepper->release();
|
|
});
|
|
|
|
OscWiFi.subscribe(recv_port, "/black/feed/*", [](const OscMessage& m) {
|
|
float tidalSpeed = m.arg<float>(0);
|
|
String address = m.address();
|
|
// Serial.println(address);
|
|
|
|
Serial.println(address);
|
|
if (address == "/black/feed/f") {
|
|
blackFeed->run(FORWARD);
|
|
} else if (address == "/black/feed/b") {
|
|
blackFeed->run(BACKWARD);
|
|
} else if (address == "/black/feed/r") {
|
|
blackFeed->run(RELEASE);
|
|
} else if (address == "/black/feed/speed") {
|
|
blackFeedSpeed = tidalSpeed;
|
|
blackFeed->setSpeed(blackFeedSpeed);
|
|
} else {
|
|
Serial.println("dont know what to do with this OSC");
|
|
};
|
|
});
|
|
|
|
OscWiFi.subscribe(recv_port, "/black/head/*", [](const OscMessage& m) {
|
|
float tidalSpeed = m.arg<float>(0);
|
|
String address = m.address();
|
|
// Serial.println(address);
|
|
|
|
Serial.println(address);
|
|
if (address == "/black/head/f") {
|
|
blackHead->run(FORWARD);
|
|
} else if (address == "/black/head/b") {
|
|
blackHead->run(BACKWARD);
|
|
} else if (address == "/black/head/r") {
|
|
blackHead->run(RELEASE);
|
|
} else if (address == "/black/head/speed") {
|
|
blackHeadSpeed = tidalSpeed;
|
|
blackHead->setSpeed(blackHeadSpeed);
|
|
} else {
|
|
Serial.println("dont know what to do with this OSC");
|
|
};
|
|
});
|
|
|
|
OscWiFi.subscribe(recv_port, "/stepper/*", [](const OscMessage& m) {
|
|
String address = m.address();
|
|
|
|
Serial.println(address);
|
|
if (address == "/stepper/f") {
|
|
stepper->step(stepperSteps, FORWARD, stepperType);
|
|
} else if (address == "/stepper/b") {
|
|
stepper->step(stepperSteps, BACKWARD, stepperType);
|
|
} else if (address == "/stepper/r") {
|
|
stepper->release();
|
|
} else if (address == "/stepper/mode") {
|
|
stepperType = m.arg<int>(0);
|
|
Serial.print("setting step type to");
|
|
Serial.println(stepperType);
|
|
} else if (address == "/stepper/steps") {
|
|
stepperSteps = m.arg<float>(0);
|
|
Serial.print("setting steps");
|
|
Serial.println(stepperSteps);
|
|
} else if (address == "/stepper/rpm") {
|
|
stepperRPM = m.arg<float>(0);
|
|
Serial.print("setting RPM");
|
|
Serial.println(stepperRPM);
|
|
stepper->setSpeed(stepperRPM);
|
|
} else {
|
|
Serial.println("dont know what to do with this OSC");
|
|
};
|
|
});
|
|
}
|
|
|
|
void loop() {
|
|
OscWiFi.update();
|
|
}
|