Ускорение и замедление dc мотора
- Войдите на сайт для отправки комментариев
Чт, 29/11/2012 - 01:16
Два dс мотора управляются с помощью джойстика и ардуино. Как реализовать плавное увеличение и уменьшение скорости вращения двигателей при резком изменении положения джойстика (ramping).
// X axis is A0
// Y axis is A1
#define switchPin 2 // switch input
#define motor1Pin 3 // Motor 1 leg 1 (pin 2, 1A)
#define motor2Pin 4 // Motor 1 leg 2 (pin 7, 2A)
#define motor3Pin 6 // Motor 2 leg 1
#define motor4Pin 7 // Motor 2 leg 2
#define enable1Pin 9 // Motor1 enable pin
#define enable2Pin 10 // Motor2 enable pin
#define ledPin 13 // Reset LED
#define slack 50 // Slack in joystick
float joy0; // Defines scaling factor for joystick A0
float joy1; // Defines scaling factor for joystick A1
float joyA0; // Defines starting position of X axis
float joyA1; // Defines starting position of Y axis
void setup()
{
// Sets scaling factors at program start
joy0 = 1023/analogRead(A0);
joy1 = 1023/analogRead(A1);
// Determines and reads starting positions of X and Y axis
joyA0 = analogRead(A0);
joyA1 = analogRead(A1);
// set the switches as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(enable2Pin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:
// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
Serial.begin(9600); //Turn on the Serial Port at 9600 bps
}
void loop()
{
// Reads position of A0 and A1 (X and Y axis)
int stickX = analogRead(A0);
int stickY = analogRead(A1);
// Reads the position of A0 and determines direction and speed
if (stickX < joyA0-slack)
{
analogWrite(enable1Pin, abs(stickX-510)/2);
digitalWrite(motor1Pin, HIGH); // set leg 1 of the Motor1 high
digitalWrite(motor2Pin, LOW); // set leg 2 of the Motor1 low
}
if (abs( stickX - joyA0 ) < slack) // Sets deadspace on stick
{
digitalWrite(enable1Pin, LOW);
}
if (stickX > joyA0+slack)
{
analogWrite(enable1Pin, (stickX-512)/2);
digitalWrite(motor1Pin, LOW); // set leg 1 of the Motor1 low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the Motor1 high
}
// Reads the position of A1 and determines direction and speed
if (stickY < joyA1-slack)
{
analogWrite(enable2Pin, abs(stickY-510)/2);
digitalWrite(motor3Pin, HIGH); // set leg 1 of the Motor2 high
digitalWrite(motor4Pin, LOW); // set leg 2 of the Motor2 low
}
if (abs(stickY - joyA1) < slack) // Sets deadspace on stick
{
digitalWrite(enable2Pin, LOW);
}
if (stickY > joyA1+slack)
{
analogWrite(enable2Pin, (stickY-513)/2);
digitalWrite(motor3Pin, LOW); // set leg 1 of the Motor2 low
digitalWrite(motor4Pin, HIGH); // set leg 2 of the Motor2 low
}
}
//blinks an LED
void blink(int whatPin, int howManyTimes, int milliSecs)
{
int i = 0;
for ( i = 0; i < howManyTimes; i++)
{
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}
Регулировка ускорения должна осуществляться с помощью потенциометра.
Здравствуйте!
Не могу запустить код, при проверке выдает ошибку (stray"#".....) ?
Кто выдает? При какой проверке? И ошибки типа dfg......... попробуйте сами разшифровать, нам за вашими точками ошибку не видно.
Не кто а что выдает ошибку, ошибку выдает Arduino 1.0.4 или на каком ресурсе мы находимся? вот ошибка-, с уважением fatman2003
sketch_mar16a:3: error: stray '#' in program
sketch_mar16a:4: error: stray '#' in program
sketch_mar16a:5: error: stray '#' in program
sketch_mar16a:6: error: stray '#' in program
sketch_mar16a:7: error: stray '#' in program
sketch_mar16a.ino:8:1: error: invalid digit "8" in octal constant
sketch_mar16a:8: error: stray '#' in program
sketch_mar16a.ino:9:1: error: invalid digit "9" in octal constant
sketch_mar16a:9: error: stray '#' in program
sketch_mar16a:10: error: stray '#' in program
sketch_mar16a:11: error: stray '#' in program
sketch_mar16a:1: error: expected unqualified-id before numeric constant
sketch_mar16a:13: error: expected unqualified-id before numeric constant
sketch_mar16a:14: error: expected unqualified-id before numeric constant
sketch_mar16a:15: error: expected unqualified-id before numeric constant
sketch_mar16a:16: error: expected unqualified-id before numeric constant
sketch_mar16a:45: error: expected unqualified-id before numeric constant
Ну так покажите свой код, потому как код выше компилируется без проблем. Вставка программного кода в тему/комментарий