Heute habe ich etwas neues probiert: Achten fahren. Dabei hat sich heraus gestellt, dass die Lenkung mittels I-Regler beim Wechsel der Lenk-Richtung zu träge ist. Ich habe darum einen P-Regler für die Lenkung hinzugefügt, nun klappt auch das.
Meine Regelung entspricht nicht ganz dem Verfahren in der Wissens-Sammlung. Das Ergebnis des I-Reglers verwende ich nicht direkt als PWM Wert, sondern ich addiere es zum bisherigen PWM Wert. Andererseits bilde ich keine Summe der Fehler aller Intervalle. Unterm Strich ergibt sich ein Regelverhalten, dass dem I Regler ähnlich ist. Auch mein P-Regler ändert nur den aktuellen PWM Wert, weswegen er zu starken Schwingungen neigt, wenn nicht zugleich der I-Regler dagegen steuert. Insgesamt klappt das Zusammenspiel des I-Regler zur Geschwindigkeitssteuerung und des PI Reglers der Lenkung nun zufriedenstellend.
Code:
#include "nibobee.h"
#include <util/delay.h>
// Fine-Tuning settings for motor control
#define ODOMETER_TICK_MM 6 // Distance of a single odometer tick in mm
#define MOTOR_START_PWM 250 // Minimum PWM value to start the motors
#define MOTOR_CONTROL_INTERVAL 100 // Interval of motor control.
// Steering control does not work, if the
// interval is too small.
#define SPEED_CONTROL_FACTOR 5 // Factor for speed correction.
// Too high value causes slipping or bucking.
#define STEERING_CONTROL_FACTOR_I 0.1 // Factor for integral steering correction.
// Too less and too high values cause swinging.
#define STEERING_CONTROL_FACTOR_P 2 // Factor for pulse steering correction.
// Too high value causes over-steering.
// How to tune steering: set FACTOR_P to 0, then increase FACTOR_I to the highest
// possible value that does not cause swinging. Then increase FACTOR_P to the
// highest possible value that does not over-steering.
// Wait for start signal (touch any sensor)
// While waiting, display debug information from sensors:
// LED0: Left odometer sensor
// LED3: Right odometer sensor
// LED1: System timer
// LED2: Center line sensor
void wait_for_start() {
while (!(SENS_SW1 || SENS_SW2 || SENS_SW3 || SENS_SW4)) {
// Display status of odometry sensors while waiting
set_LED0(ODO_L);
set_LED3(ODO_R);
// Display system timer (flashes every second)
set_LED1((system_time() % 1000)==0);
// Display line sensor
set_LED2(analog(LINE_C)>600);
}
set_LED0(0);
set_LED1(0);
set_LED2(0);
set_LED3(0);
_delay_ms(10);
while ((SENS_SW1 || SENS_SW2 || SENS_SW3 || SENS_SW4)) {}
_delay_ms(400);
}
// Drive an exact distance and accellerate/decellerate softly to the given speed.
// The distance of both wheels can be different.
// Speed control works best in range 100-800.
void drive(int32_t distance_mm_l, int32_t distance_mm_r, uint32_t speed_mm_sec) {
// Set motor direction and remove sign from the distance value
set_DIR_L(distance_mm_l > 0);
set_DIR_R(distance_mm_r < 0);
if (distance_mm_l<0) distance_mm_l *= -1;
if (distance_mm_r<0) distance_mm_r *= -1;
// Calculate the destination distance in odometer ticks
uint32_t dest_distance_l=distance_mm_l/ODOMETER_TICK_MM;
uint32_t dest_distance_r=distance_mm_r/ODOMETER_TICK_MM;
// Calculate the steering factor of left to right motor speed
double steering_factor;
if (distance_mm_l>0 && distance_mm_r>0)
steering_factor=(double) distance_mm_l / (double) distance_mm_r;
else
steering_factor=1;
// Calculate the destination speed in odometer ticks per millisecond
double dest_speed=(double) speed_mm_sec/ODOMETER_TICK_MM/1000;
// Initialize variables
reset_odometer();
uint32_t last_odo_l=0;
uint32_t last_odo_r=0;
uint32_t last_time=system_time();
// Current pwm values
uint16_t pwm_l=PWM_L;
uint16_t pwm_r=PWM_R;
// Start the motors, if necessary
if (distance_mm_l>0 && pwm_l==0)
pwm_l=MOTOR_START_PWM;
if (distance_mm_r>0 && pwm_r==0)
pwm_r=MOTOR_START_PWM;
// Drive until both motors reached the nominal distance
while (odometer_left()<dest_distance_l || odometer_right()<dest_distance_r) {
// Calculate time interval
uint32_t now=system_time();
int32_t interval_time=now-last_time;
if (interval_time>=MOTOR_CONTROL_INTERVAL) {
// Get current distance in odometer ticks
uint32_t odo_l=odometer_left();
uint32_t odo_r=odometer_right();
double delta_odo_l=(odo_l-last_odo_l);
double delta_odo_r=(odo_r-last_odo_r);
// average speed of both wheels in odometer ticks per millisecond
double distance=(delta_odo_l+delta_odo_r)/2;
double speed=distance/interval_time;
double speed_error=dest_speed-speed;
// Speed control
if (speed_error!=0) {
double value=speed_error*interval_time*SPEED_CONTROL_FACTOR;
pwm_l+=value*steering_factor;
pwm_r+=value/steering_factor;
}
// Display speed status
set_LED1(speed_error>1)
// Calculate steering error
double steering_error_i=0;
if (delta_odo_r>0 && delta_odo_l>0) {
steering_error_i=delta_odo_l-delta_odo_r*steering_factor;
}
double steering_error_p=0;
if (odo_r>0 && odo_l>0) {
steering_error_p=odo_l-odo_r*steering_factor;
}
// Integral steering control
double steering=steering_error_i*interval_time*STEERING_CONTROL_FACTOR_I+
steering_error_p*STEERING_CONTROL_FACTOR_P;
pwm_l-=steering;
pwm_r+=steering;
// Display steering status
set_LED0(steering>1);
set_LED3(steering<-1);
// Apply the new PWM values to the motor driver, while
// limiting to the allowed range.
if (pwm_l>1023) pwm_l=1023;
PWM_L=pwm_l;
if (pwm_r>1023) pwm_r=1023;
PWM_R=pwm_r;
last_time=now;
last_odo_l=odo_l;
last_odo_r=odo_r;
}
}
}
// Stop driving.
void stop() {
PWM_L=0;
PWM_R=0;
set_LED1(1);
_delay_ms(50);
set_LED1(0);
_delay_ms(50);
set_LED1(1);
_delay_ms(50);
set_LED1(0);
_delay_ms(50);
}
// Main program
int main() {
while (1) {
wait_for_start();
// Drive a few times forward and backward for 2 meters
for (int i=0; i<3; i++) {
drive(1700,1700,200);
drive(300,300,100);
stop();
_delay_ms(300);
drive(-1700,-1700,400);
drive(-300,-300,100);
stop();
_delay_ms(300);
}
// Drive forward for 1 meter
drive(700,700,200);
drive(300,300,100);
stop();
_delay_ms(300);
// Turn 180°
drive(180,-180,200);
stop();
_delay_ms(300);
// Drive back
drive(700,700,200);
drive(300,300,100);
// Drive an eight
uint32_t distance=1450;
for (int i=0; i<3; i++) {
drive(distance,distance/2,200);
drive(distance/2,distance,200);
}
stop();
}
return 0;
}
Lesezeichen