Outils pour utilisateurs

Outils du site


esp8266:servoeau

Commande de servo-moteur OSC ESP8266

en code arduino pour test

// servo moteur a commande OSC
// WIFI
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// OSC
#include <OSCMessage.h>
#include <OSCBoards.h>
//port numbers for OSC

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
const int servopin = 4;  //( 5 sur esp )
int pos;
  
#define MONIT true // debug serie

const float Version_prog = 0.01 ;

const unsigned int inPort = 9081;
//destination IP pour la trace OSC
IPAddress MonitIP(192, 168, 0, 99); // IP et port du moniteur
const unsigned int MonitPort = 9998; // suivi normal
IPAddress DebugIP(192, 168, 0, 123); // IP et port de debug
const unsigned int DebugPort = 9999;
WiFiUDP udpin;
WiFiUDP udpout;
const char* ssid     = "ssid";
const char* password = "blablabla";
char* nomhost  = "SERVO-1"; // le nom iendra de l'ip
char* nomservo = "SERVO-"; // debut du nom du servo
int noservo = 1; // no du servo
char* OSCVar = "/SERVO-1";

// DHCP donc pas besoin
//IP
//IPAddress (192,168,IPC,IPD);
int IPC = 0;
int IPD = 81;

void Angle(OSCMessage &msg) {
  int angle = msg.getInt(0);
  if( angle>= 0 && angle <= 180) { // FUTABA pour nous 23-153
    myservo.write(angle);   
    MonitMsg("angle", angle); 
  }
  
}

bool MonitMsg(char debugbla[], int debugint) {
  sprintf(OSCVar, "/SERVO-%d/%s", noservo, debugbla);
  if (MONIT) {
    Serial.println("");
    Serial.println(OSCVar);
  }
  OSCMessage envoi(OSCVar);
  //envoi.add(debugbla);
  envoi.add(debugint);

  udpout.beginPacket(MonitIP, MonitPort);
  envoi.send(udpout);
  udpout.endPacket();

  udpout.beginPacket(DebugIP, DebugPort);
  envoi.send(udpout);
  udpout.endPacket();
  envoi.empty();
}

void setup() {
  Serial.begin(115200);
  Serial.println(ESP.getChipId(), HEX);
  //setup ethernet part
  Serial.println();
  Serial.print("servo-eau ");
  Serial.println(Version_prog);

  Serial.print("Connecting to SSID ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  //WiFi.hostname(nomhost);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  //  DHCP donc pas fixé ici
  //WiFi.config(IPAddress(192, 168, IPC, IPD), IPAddress(192, 168, IPC, IPD), IPAddress(255, 255, 255, 0));
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(ssid);
  Serial.println("IP address: ");
  IPAddress IPLOCAL = WiFi.localIP();
  IPD = (int)IPLOCAL[3];
  Serial.println(IPD);
  Serial.println(IPLOCAL);
  //noservo = abs(IPD - 80);
  //Serial.println(WiFi.macAddress);
  sprintf(nomservo, "SERVO-%d", noservo);
  sprintf(OSCVar, "/SERVO-%d", noservo);
  Serial.println("hostname: ");
  Serial.println(nomservo);
  WiFi.hostname(nomservo);
  Serial.println(WiFi.hostname());
  Serial.println("Starting UDP");
  udpin.begin(inPort);
  Serial.print("Local port: ");
  Serial.println(udpin.localPort());

  delay(100);
  // envoi le bonjour
  MonitMsg("BONJOUR", (int32_t)ESP.getChipId());
  MonitMsg("version", (int)(Version_prog * 100));
  MonitMsg("servopin", servopin); 
  
  myservo.attach(servopin);  // attaches the servo  
  
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  myservo.write(0);
  delay(500); 
    myservo.write(90);
  delay(500); 
    myservo.write(180);
  delay(500); 
}


void loop() {
  /*    for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  */
  // Lire la comande et executer le debut
  OSCMessage msgIN;
  int size;
  char puce_char[] = "  ";
  if ( (size = udpin.parsePacket()) > 0)
  {
    while (size--)
      msgIN.fill(udpin.read());

    if (!msgIN.hasError()) {
      // /stop immediat moteur et commande
      msgIN.dispatch("/angle", Angle);  // commande=0
    }
    udpin.flush();
  }

}

et donc

oscsend 192.168.0.165 9081 /angle i 90
sleep 1 
oscsend 192.168.0.165 9081 /angle i 23
sleep 1
oscsend 192.168.0.165 9081 /angle i 155
esp8266/servoeau.txt · Dernière modification: 2017/04/11 15:13 (modification externe)