|
|

楼主 |
发表于 2014-2-7 21:54:09
|
显示全部楼层
本帖最后由 czh007007 于 2014-2-7 21:55 编辑
隨風大俠 发表于 2014-2-7 20:04 
你程式碼可以貼出來讓大家看看嗎??這樣比較好幫。
全部就有点长了,全文如下:
/*
This example shows how to use PWM to control the motors
using the analogWrite() function.
*/
int IRpin = 1;
#define motor1A 3
#define motor1C 5
#define motor2A 6
#define motor2C 9
#define motor3A 10
#define motor3C 11
#define clkwise 0
#define anticlkwise 1
#define M1
#define M2
#define M3
#define cos60 0.5
#define sin60 0.866
float a1_sp;
float a2_sp;
float a3_sp;
char a1_speed;
char a2_speed;
char a3_speed;
int halt = 0; // how bright the LED is
int incAmount = 5; // how many points to fade the LED by
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// declare pin 9 to be an output:
pinMode(motor1A, OUTPUT);
pinMode(motor1C, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2C, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3C, OUTPUT);
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(20);
}
void loop() {
// call motor routine:
float distance = 65*pow(analogRead(IRpin)*0.0048828125, -1.10);
Serial.println(65*pow(analogRead(IRpin)*0.0048828125, -1.10));
while(65*pow(analogRead(IRpin)*0.0048828125, -1.10)>20)
movement();
while(65*pow(analogRead(IRpin)*0.0048828125, -1.10) > 10 && 65*pow(analogRead(IRpin)*0.0048828125, -1.10) < 20)
{
analogWrite(motor1A,0);
analogWrite(motor1C,0);
analogWrite(motor2A,0);
analogWrite(motor2C,0);
analogWrite(motor3A,0);
analogWrite(motor3C,0);
}
}
void movement()
{
spin(100,5000);
forward(100, 5000);
rightward(100,4000);
}
void control(char robospeed, float roboangle, char roborotate ) {
// assign the correct output pin for motor_num
// v.v1 + roborotate
a1_sp= - cos60*robospeed*cos(roboangle) + sin60*robospeed*sin(roboangle) + roborotate;
// v.v2+ roborotate
a2_sp= 1*robospeed*cos(roboangle) + roborotate;
// v.v3+ roborotate
a3_sp= - cos60*robospeed*cos(roboangle) - sin60*robospeed*sin(roboangle) + roborotate;
#ifdef M1
if (a1_sp > 0) {
a1_speed = a1_sp;
motor(1, a1_speed, clkwise);
}
else {
a1_sp *=-1;
a1_speed = a1_sp;
motor(1, a1_speed, anticlkwise);
}
#endif
#ifdef M2
if (a2_sp > 0) {
a2_speed = a2_sp;
motor(2, a2_speed, clkwise);
}
else {
a2_sp *=-1;
a2_speed = a2_sp;
motor(2, a2_speed, anticlkwise);
}
#endif
#ifdef M3
if (a3_sp > 0) {
a3_speed = a3_sp;
motor(3, a3_speed, clkwise);
}
else {
a3_sp *=-1;
a3_speed = a3_sp;
motor(3, a3_speed, anticlkwise);
}
#endif
}
/*
Setting the speed and direct control of a specific motor
*/
void motor(char motor_num, char speed_val, boolean direct ) {
char motorLA;
char motorLC;
// assign the correct output pin for motor_num
switch(motor_num) {
case 1:
motorLA = motor1A;
motorLC = motor1C;
break;
case 2:
motorLA = motor2A;
motorLC = motor2C;
break;
case 3:
motorLA = motor3A;
motorLC = motor3C;
break;
}
if (direct) { // direct = 0:clockwise direct = 1:anti-clockwise
analogWrite(motorLA, 0);
analogWrite(motorLC, speed_val);
}
else {
analogWrite(motorLC, 0);
analogWrite(motorLA, speed_val);
}
}
void forward(int speedval, int time)
{
control (speedval, 3.141*0.5, 0);
delay(time);
}
void rightward(int speedval,int time)
{
control (speedval, 0, 0);
delay(time);
}
void spin(int speedval, int time)
{
analogWrite(motor1A,speedval);
analogWrite(motor1C,0);
analogWrite(motor2A,speedval);
analogWrite(motor2C,0);
analogWrite(motor3A,speedval);
analogWrite(motor3C,0);
delay(time);
}
|
|