folgenden code habe ich nun getestet:
(servo angeschlossen an XBUS1 1,3,12)
Code:
/*
* ****************************************************************************
* RP6 ROBOT SYSTEM - ROBOT BASE EXAMPLES
* ****************************************************************************
* Example: Simple Servo Example
* Author(s): D. Ottensmeyer
* ****************************************************************************
* Description:
* This example shows how to use a standard servo with RP6Library.
* There are much better solutions to do this job, but you may use
* this simple program to make some first experiments with servos.
* The servo is connected to PINC1 (SDA).
*
* ############################################################################
* The Robot does NOT move in this example! You can simply put it on a table
* next to your PC and you should connect it to the PC via the USB Interface!
* ############################################################################
* ****************************************************************************
*/
/*****************************************************************************/
// Includes:
#include "RP6RobotBaseLib.h" // The RP6 Robot Base Library.
// Always needs to be included!
/*****************************************************************************/
// Defines:
// RP6 Base Servo Connection:
#define SERVO_OUT SDA // PINC1 XBUS Pin 12
// Servo movement limits (depending on servo type):
#define LEFT_TOUCH 100 // Left servo touch
#define RIGHT_TOUCH 50 // Right servo touch [max. 255]
#define PULSE_ADJUST 4
#define PULSE_REPETITION 19 // Pulse repetition frequency
/*****************************************************************************/
// Functions:
/**
* INIT SERVO
*
* Call this once before using the servo task.
*
*/
void initSERVO(void)
{
DDRC |= SERVO_OUT; // SERVO_OUT -> OUTPUT
PORTC &= ~SERVO_OUT; // SERVO_OUT -> LO
startStopwatch1(); // Needed for 20ms pulse repetition
}
/**
* PULSE SERVO
*
* This is the servo pulse generation. This function
* must be called every 20ms (pulse repetition).
*
* position = 0 : Left touch
* position = RIGHT_TOUCH : Right touch
*
* ATTENTION: ! This function is BLOCKING all other activities for about 1 !
* ! to 2ms (depending on the value of position)!!! !
* ! If you generate a pulse every 20ms 5-10% of the processor's !
* ! calculating time is wasted by this kind of pulse generation. !
* ! If this is a problem for the rest of your program, you !
* ! cannot use this method. !
* ! You will need an interrupt-based solution instead. !
*
*/
void pulseSERVO(uint8_t position)
{
cli();
// Impulsstart, Ausgang auf High
PORTC |= SERVO_OUT;
// Der Gesamtimpuls besteht aus zwei Teilen, zuerst der Anteil für linke Position
delayCycles(LEFT_TOUCH);
// und dann der Abstand von Links zum gewünschten Drehwinkel in Einzelschritten*pos
while (position--) {
delayCycles(PULSE_ADJUST);
}
// Impuls ist gesendet, Ausgang wieder auf Low
PORTC &= ~SERVO_OUT; // SERVO_OUT -> LO (pulse end)
sei();
}
/**
* SERVO TASK
*
* This is the servo demo task.
* The positioning demo shows the servo lever rapidly
* moving to the left touch, then slowly moving to
* the right touch and so on ...
*
*/
void task_SERVO(void)
{static uint8_t pos;
// wenn Stopwatch1 größer 19 ist sind 20ms seit dem letzten Impulsstart vergangen
if (getStopwatch1() > PULSE_REPETITION) {
//Impuls zum Servo senden
pulseSERVO(pos);
// ---------------------------------------------------------------------
// Nach ca. 50ms wird der Impuls für einen neuen Winkel berechnet
if (getStopwatch2() > 48) {
pos++; // Impulslänge um einen Schritt erhöhen
//pos+=3; // größere Schritte
// Wenn der rechte Endpunkt erreicht ist, starten wir wieder links
if (pos > RIGHT_TOUCH) {pos = 0;}
setStopwatch2(0); // restart der 50ms-Verzögerung
}
// ---------------------------------------------------------------------
setStopwatch1(0); // restart der 20ms-Impulswiederholung
}
}
/*****************************************************************************/
// Main function - The program starts here:
int main(void)
{
initRobotBase(); // Always call this first! The Processor will not work
// correctly otherwise.
// ---------------------------------------
// Write messages to the Serial Interface:
writeString_P("\n\n _______________________\n");
writeString_P(" \\| RP6 ROBOT SYSTEM |/\n");
writeString_P(" \\_-_-_-_-_-_-_-_-_-_/\n\n");
writeString_P("################\n");
writeString_P("<<RP6 Base>>\n");
writeString_P(" Servo - Test 1 \n");
writeString_P(" Version 1.00 \n");
writeString_P("################\n\n");
mSleep(2500);
setLEDs(0b111111); // Turn all LEDs on
mSleep(500); // delay 500ms
setLEDs(0b000000); // All LEDs off
initSERVO();
startStopwatch2(); // Used for the demo task
while(true)
{
task_SERVO();
task_RP6System();
}
return 0;
}
/******************************************************************************
* Additional info
* ****************************************************************************
* Changelog:
* - v. 1.0 (initial release) 03.10.2007 by D. Ottensmeyer
*
* ****************************************************************************
*/
/*****************************************************************************/
- beim einschalten lässt sich der servo beliebig durchdrehen (ist spannungsfrei), nur durch die mechanischen anschläge begrenzt, grüne LED1 blinkt
- druck auf den taster: rechte position (oder die nächste?) wird gesucht, elektrischer "anschlag" gefunden, reset (alle 6 LED´s leuchten auf), servo steht unter spannung, grüne LED1 blinkt
- beim nächsten druck auf den taster wieder reset, servo zuckt nach links, bleibt dann aber stehen
der letzte punkt wiederholt sich bei jedem druck auf den taster
Lesezeichen