Servo Position-Halten - Adruino Nano

Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#16 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

ich hatte hier doch noch irgendwo einen nagelneuen nano rumfliegen....... gefunden...... pin headers anlöten.......

Die Arduino IDE hast du schon installiert?

Dann mach mal mit:
https://github.com/scottjgibson/RCArduinoFastLib rechts auf "Clone or download" dann "Download ZIP".

Das scheint ne alte Library zu sein, jedenfalls gelang es mir nicht diese direkt in der Arduino IDE zu installieren.
Das müsste aber auch manuell gehen.
Aus dem .zip kopierst du RCArduinoFastLib.cpp und RCArduinoFastLib.h und PinChangeInt.h nach
C:\Users\Chris\Documents\Arduino\libraries\RCArduinoFastLib
(natürlich mit deinem User, nicht mit "Chris")
Dann kopierst du \RCArduinoFastLib-master\example\src\sketch.ino nach
C:\Users\Chris\Documents\Arduino\libraries\RCArduinoFastLib\examples
In der IDE muss unter "Werkzeug" das Board "Arduino nano" ausgewählt sein.
Dann öffnest du das sketch.ino
Dann klickst du auf "Sketch" -> "Überprüfen/Kompilieren".
Hier angekommen kompiliert das Sketch bei mir schon mal.

Kommst du bis dahin mit?
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#17 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Ich musste noch einen CH340 Treiber installieren.
Das hab ich nach dieser Anleitung auf chinesisch gemacht :-)
https://iotrobots.wordpress.com/2015/11 ... g-ch341-g/
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#18 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Ich musste in der IDE noch unter "Werkzeuge" -> "Prozessor" auf "ATmega328p (Old Bootloader)" umstellen.
Dann konnte ich ein kleines Testsketch erfolgreich hochladen:

Code: Alles auswählen

byte LED1 = 13;      // Arduino Pin vom der Arduino nano onboard LED

void setup() {
  pinMode(LED1, OUTPUT);
}

void loop() {
  digitalWrite(LED1, HIGH);  // LED1 einschalten
  delay(50);                // 50 Millisekunden warten
  digitalWrite(LED1, LOW);   // LED1 ausschalten
  delay(50);                // 50 Millisekunden warten
}
Kommst du soweit mit?
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#19 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

ich versteh es zwar noch nicht so richtig aber ich bin soweit. Nur leider ist mein Nano noch auf dem Weg. Also mit hochladen wird das noch nix. :oops:

Aber Kompilieren wurde abgeschlossen und ich habe den schreibgeschützten Sketch in einen Ordner Sketch_2 neu gespeichert!?! Soll ich dann den Inhalt im Sketch gegen das von dir über die LED Ansteuerung ersetzen???
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#20 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Das LED Blink Sketch oben dient nur dazu um zu überprüfen, ob der Sketch hochlädt und die Onboard LED auf dem Nano blinkt. Dazu ist keine weitere Hardware nötig und man testen, ob bis dahin alles funktioniert.
Wenn es das tut, kann man mit dem eigentlichen Programm anfangen.
Das du bis zum kompilieren kommst ist schon mal ein sehr gutes Zeichen :-)

Ich bin gerade dabei das Beispiel von der Bibliothek auseinanderzunehmen, zu verstehen und dann so umzustricken, dass dabei rauskommt was du willst. Ich habe leider nur zwei Servos greifbar und keinen Empfänger angesteckt.
Ich habe aber noch das Problem, dass die Ausgänge noch nicht richtig funktionieren, "D2" kennt der nicht, dass muss anders heißen, ich suche gerade noch.....
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#21 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

ich glaube man schreibt sie nicht mit D2 an sondern mit den Zahlen in der Klammer!?!
NANO3C%20-%20Layout01.jpg
Mach dir kein Stress das Nano kommt mit Glück Ende kommender Woche...
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#22 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

So, nu laufen schon mal die beiden Servos:
Dazu muss du evtl. noch die Servo Bibliothek installieren, falls sie noch nicht installiert ist.
(Wir sind noch im Testbetrieb um uns langsam und schrittweise an die Lösung ranzutasten....)

Code: Alles auswählen

#include <Servo.h>
Servo Servo1;
Servo Servo2;
void setup()
{
  Servo1.attach(2);
  Servo2.attach(3);
}
void loop()
{
  int pos;
  for (pos = 0; pos <= 180; pos += 5)
  {
    Servo1.write(pos);
    delay(100);
  }
  for (pos = 180; pos >= 0; pos -= 10)
  {
    Servo2.write(pos);
    delay(100);
  }
}
Für Pin "D2" muss man im Code "2" schreiben.
Für Pin "D3" muss man im Code "3" schreiben.

Heisst also, dass die Servos schon mal mit einem Arduino nano bewegt werden können :-)
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#23 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

TheManFromMoon hat geschrieben:So, nu laufen schon mal die beiden Servos:
Dazu muss du evtl. noch die Servo Bibliothek installieren, falls sie noch nicht installiert ist.
(Wir sind noch im Testbetrieb um uns langsam und schrittweise an die Lösung ranzutasten....)

Code: Alles auswählen

#include <Servo.h>
Servo Servo1;
Servo Servo2;
void setup()
{
  Servo1.attach(2);
  Servo2.attach(3);
}
void loop()
{
  int pos;
  for (pos = 0; pos <= 180; pos += 5)
  {
    Servo1.write(pos);
    delay(100);
  }
  for (pos = 180; pos >= 0; pos -= 10)
  {
    Servo2.write(pos);
    delay(100);
  }
}
Für Pin "D2" muss man im Code "2" schreiben.
Für Pin "D3" muss man im Code "3" schreiben.

Heisst also, dass die Servos schon mal mit einem Arduino nano bewegt werden können :-)
Bist dir da sicher? Ich glaube eher:
D2 = Pin "5"
D3= "Pin "6"
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#24 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Ja, bin ich mir sicher, habe es ja getestet.
Für "D2" muss man "2" im Code schreiben. Die "5" ist einfach nur die durchnummerierte Hardware Pin-Nummer.
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#25 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

So, nun hab ich mal meinen Sender und einen Empfänger rausgeholt und angeschlossen.
Also:
Sender -> Empfänger -> Arduino nano -> Servos

Das läuft!

Aber extrem beschissen, nicht zu gebrauchen :-(

Zum einen reagieren die Servos viel zu langsam und zum anderen auch nicht sauber, manchmal springen die Servos irgendwo hin.
Ich habe dann noch das delay(500) auskommentiert.
Das war schon besser, aber immer noch kein "Servo hängt am Knüppel" Feeling.
Diese Lib scheint eine Sackgasse zu sein.
Trotzdem hier der Code bis dahin:

Code: Alles auswählen

#include <RCArduinoFastLib.h>
// MultiChannels
//
// rcarduino.blogspot.com
//
// A simple approach for reading three RC Channels using pin change interrupts
//
// See related posts -
// http://rcarduino.blogspot.co.uk/2012/01/how-to-read-rc-receiver-with.html
// http://rcarduino.blogspot.co.uk/2012/03/need-more-interrupts-to-read-more.html
// http://rcarduino.blogspot.co.uk/2012/01/can-i-control-more-than-x-servos-with.html
//
// rcarduino.blogspot.com
//

// include the pinchangeint library - see the links in the related topics section above for details
#include <PinChangeInt.h>

// Assign your channel in pins
#define SERVO1_IN_PIN 12
#define SERVO2_IN_PIN 11
#define SERVO3_IN_PIN 10
#define LED_IN_PIN 9

// Assign your channel out pins
#define SERVO1_OUT_PIN 2
#define SERVO2_OUT_PIN 3
#define SERVO3_OUT_PIN 4
#define LED_OUT_PIN 5

// Assign servo indexes
#define SERVO1 0
#define SERVO2 1
#define LED 2
#define SERVO_FRAME_SPACE 3

// These bit flags are set in bUpdateFlagsShared to indicate which
// channels have new signals
#define SERVO1_FLAG 1
#define SERVO2_FLAG 2
#define SERVO3_FLAG 3
#define LED_FLAG 4

// holds the update flags defined above
volatile uint8_t bUpdateFlagsShared;

// shared variables are updated by the ISR and read by loop.
// In loop we immediatley take local copies so that the ISR can keep ownership of the
// shared ones. To access these in loop
// we first turn interrupts off with noInterrupts
// we take a copy to use in loop and the turn interrupts back on
// as quickly as possible, this ensures that we are always able to receive new signals
volatile uint16_t unThrottleInShared;
volatile uint16_t unSteeringInShared;
volatile uint16_t unAuxInShared;

// These are used to record the rising edge of a pulse in the calcInput functions
// They do not need to be volatile as they are only used in the ISR. If we wanted
// to refer to these in loop and the ISR then they would need to be declared volatile
uint16_t unThrottleInStart;
uint16_t unSteeringInStart;
uint16_t unAuxInStart;

uint16_t unLastAuxIn = 0;
uint32_t ulVariance = 0;
uint32_t ulGetNextSampleMillis = 0;
uint16_t unMaxDifference = 0;

void setup()
{
  Serial.begin(115200);

  Serial.println("multiChannels");

  // attach servo objects, these will generate the correct
  // pulses for driving Electronic speed controllers, servos or other devices
  // designed to interface directly with RC Receivers
  CRCArduinoFastServos::attach(SERVO1,SERVO1_OUT_PIN);
  CRCArduinoFastServos::attach(SERVO2,SERVO2_OUT_PIN);
  CRCArduinoFastServos::attach(LED,LED_OUT_PIN);
 
  // lets set a standard rate of 50 Hz by setting a frame space of 10 * 2000 = 3 Servos + 7 times 2000
  CRCArduinoFastServos::setFrameSpaceA(SERVO_FRAME_SPACE,7*2000);

  CRCArduinoFastServos::begin();
 
  // using the PinChangeInt library, attach the interrupts
  // used to read the channels
  PCintPort::attachInterrupt(SERVO1_IN_PIN, calcThrottle,CHANGE);
  PCintPort::attachInterrupt(SERVO2_IN_PIN, calcSteering,CHANGE);
  PCintPort::attachInterrupt(SERVO3_IN_PIN, calcSteering,CHANGE);
  PCintPort::attachInterrupt(LED_IN_PIN, calcAux,CHANGE);
}

void loop()
{
  // create local variables to hold a local copies of the channel inputs
  // these are declared static so that thier values will be retained
  // between calls to loop.
  static uint16_t unThrottleIn;
  static uint16_t unSteeringIn;
  static uint16_t unAuxIn;
  // local copy of update flags
  static uint8_t bUpdateFlags;

  // check shared update flags to see if any channels have a new signal
  if(bUpdateFlagsShared)
  {
    noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables

    // take a local copy of which channels were updated in case we need to use this in the rest of loop
    bUpdateFlags = bUpdateFlagsShared;
  
    // in the current code, the shared values are always populated
    // so we could copy them without testing the flags
    // however in the future this could change, so lets
    // only copy when the flags tell us we can.
  
    if(bUpdateFlags & SERVO1_FLAG)
    {
      unThrottleIn = unThrottleInShared;
    }
  
    if(bUpdateFlags & SERVO2_FLAG)
    {
      unSteeringIn = unSteeringInShared;
    }
  
    if(bUpdateFlags & LED_FLAG)
    {
      unAuxIn = unAuxInShared;
    }
   
    // clear shared copy of updated flags as we have already taken the updates
    // we still have a local copy if we need to use it in bUpdateFlags
    bUpdateFlagsShared = 0;
  
    interrupts(); // we have local copies of the inputs, so now we can turn interrupts back on
    // as soon as interrupts are back on, we can no longer use the shared copies, the interrupt
    // service routines own these and could update them at any time. During the update, the
    // shared copies may contain junk. Luckily we have our local copies to work with :-)
  }

  // do any processing from here onwards
  // only use the local values unAuxIn, unThrottleIn and unSteeringIn, the shared
  // variables unAuxInShared, unThrottleInShared, unSteeringInShared are always owned by
  // the interrupt routines and should not be used in loop

  // the following code provides simple pass through
  // this is a good initial test, the Arduino will pass through
  // receiver input as if the Arduino is not there.
  // This should be used to confirm the circuit and power
  // before attempting any custom processing in a project.

  // we are checking to see if the channel value has changed, this is indicated
  // by the flags. For the simple pass through we don't really need this check,
  // but for a more complex project where a new signal requires significant processing
  // this allows us to only calculate new values when we have new inputs, rather than
  // on every cycle.
  if(bUpdateFlags & SERVO1_FLAG)
  {
    CRCArduinoFastServos::writeMicroseconds(SERVO1,unThrottleIn);
  }

  if(bUpdateFlags & SERVO2_FLAG)
  {
    CRCArduinoFastServos::writeMicroseconds(SERVO2,unSteeringIn);
  }

  if(bUpdateFlags & LED_FLAG)
  {
   CRCArduinoFastServos::writeMicroseconds(LED,unAuxIn);
  }

  //delay(500);
  bUpdateFlags = 0;
}


// simple interrupt service routine
void calcThrottle()
{
  if(PCintPort::pinState)
  {
    unThrottleInStart = TCNT1;
  }
  else
  {
    unThrottleInShared = (TCNT1 - unThrottleInStart)>>1;
    bUpdateFlagsShared |= SERVO1_FLAG;
  }
}

void calcSteering()
{
  if(PCintPort::pinState)
  {
    unSteeringInStart = TCNT1;
  }
  else
  {
    unSteeringInShared = (TCNT1 - unSteeringInStart)>>1;

    bUpdateFlagsShared |= SERVO2_FLAG;
  }
}

void calcAux()
{
  if(PCintPort::pinState)
  {
    unAuxInStart = TCNT1;
  }
  else
  {
    unAuxInShared = (TCNT1 - unAuxInStart)>>1;
    bUpdateFlagsShared |= LED_FLAG;  }
}
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#26 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

o Mann ich würd das so gerne testen. Aber ohne Nano geht's schlecht... :cry:

Mach dir echt kein Stress auch wenn ich es geil finde wie du Gas gibst :-)
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#27 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Das Problem ist: Ich habe extrem selten Zeit. Jetzt passte das zufällig mal. Wann ich mich das nächste mal damit beschäftigen kann, weiß ich noch nicht. Das kann Wochen dauern. Werden wir sehen.
Ich habe mal im Arduino Forum gefragt welche Lib man für diese Anwendung empfehlen würde. Mal sehen, was da kommt.

Lieben Gruß,
Chris
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#28 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

TheManFromMoon hat geschrieben:Das Problem ist: Ich habe extrem selten Zeit. Jetzt passte das zufällig mal. Wann ich mich das nächste mal damit beschäftigen kann, weiß ich noch nicht. Das kann Wochen dauern. Werden wir sehen.
Ich habe mal im Arduino Forum gefragt welche Lib man für diese Anwendung empfehlen würde. Mal sehen, was da kommt.

Lieben Gruß,
Chris
schon mal1000 Dank im voraus!!! :P
CubaLibreee
Beiträge: 1947
Registriert: 08.09.2013 15:35:26

#29 Re: Servo Position-Halten - Adruino Nano

Beitrag von CubaLibreee »

Ich bin ich noch relativ Planlos aber schau dir doch mal das an: https://youtu.be/hXvpafOKALE

Ab 2:41min wird das doch interessant oder?
Benutzeravatar
TheManFromMoon
Beiträge: 3235
Registriert: 19.01.2005 11:05:48
Wohnort: Bad Malente
Kontaktdaten:

#30 Re: Servo Position-Halten - Adruino Nano

Beitrag von TheManFromMoon »

Das haben wir schon in Post #22
Antworten

Zurück zu „Servos“