Rest 阿杜伊诺;CC3000&;Motorshield&;机器人
我有一辆4电机驱动的汽车,上面有ARD MEGA/CC3000/Motorshield。 我已经写了一个运行REST的草图和一个带有按钮的网页,它可以很好地连续工作。 我写了一个草图,可以控制马达的前进/后退/左/右,效果很好。 当我把两张草图结合起来,按下网页上的“前进”键时,它会工作,但会“挂起”Rest 阿杜伊诺;CC3000&;Motorshield&;机器人,rest,arduino,Rest,Arduino,我有一辆4电机驱动的汽车,上面有ARD MEGA/CC3000/Motorshield。 我已经写了一个运行REST的草图和一个带有按钮的网页,它可以很好地连续工作。 我写了一个草图,可以控制马达的前进/后退/左/右,效果很好。 当我把两张草图结合起来,按下网页上的“前进”键时,它会工作,但会“挂起” void loop() { // Handle any multicast DNS requests mdns.update(); Adafru
void loop()
{
// Handle any multicast DNS requests
mdns.update();
Adafruit_CC3000_ClientRef client = restServer.available();
rest.handle(client);
wdt_reset();
if(!cc3000.checkConnected()){while(1){}}
wdt_reset();
}
int forward(String command) {
goforwards();
return 1;
}
int backward(String command) {
gobackwards();
return 1;
}
int left(String command) {
Serial.println(F("GO LEFT..."));
//goleft();
return 1;
}
int right(String command) {
Serial.println(F("GO RIGHT..."));
//goright();
return 1;
}
int estop(String command) {
Serial.println(F("Asked to STOP..."));
allstop();
return 1;
}
void goforwards() {
Serial.println(F("GO FORWARD..."));
motor(1, FORWARD, 150); motor(4, FORWARD, 150); motor(2, FORWARD, 150); motor(3, FORWARD, 150); delay(500);
allstop();
}
void gobackwards() {
Serial.println(F("GO BACKWARD..."));
motor(1, BACKWARD, 150); motor(4, BACKWARD, 150); motor(2, BACKWARD, 150); motor(3, BACKWARD, 150); delay(500);
allstop();
}
void goleft() {motor(1, FORWARD, 175); motor(4, FORWARD, 175); motor(2, FORWARD, 50); motor(3, FORWARD, 50);delay(2000); }
void goright() {motor(1, FORWARD, 50); motor(4, FORWARD, 50); motor(2, FORWARD, 175); motor(3, FORWARD, 175); delay(2000); }
void allstop() {motor(1, RELEASE, 0); motor(2, RELEASE, 0);motor(3, RELEASE, 0);motor(4, RELEASE, 0);delay(1); }
void motor(int nMotor, int command, int speed)
{
int motorA, motorB;
if (nMotor >= 1 && nMotor <= 4)
{
switch (nMotor)
{
case 1:
motorA = MOTOR1_A;
motorB = MOTOR1_B;
break;
case 2:
motorA = MOTOR2_A;
motorB = MOTOR2_B;
break;
case 3:
motorA = MOTOR3_A;
motorB = MOTOR3_B;
break;
case 4:
motorA = MOTOR4_A;
motorB = MOTOR4_B;
break;
default:
break;
}
switch (command)
{
case FORWARD:
motor_output (motorA, HIGH, speed);
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
case BACKWARD:
motor_output (motorA, LOW, speed);
motor_output (motorB, HIGH, -1); // -1: no PWM set
break;
case BRAKE:
motor_output (motorA, LOW, 255); // 255: fully on.
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
case RELEASE:
motor_output (motorA, LOW, 0); // 0: output floating.
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
default:
break;
}
}
}
void motor_output (int output, int high_low, int speed)
{
int motorPWM;
switch (output)
{
case MOTOR1_A:
case MOTOR1_B:
motorPWM = MOTOR1_PWM;
break;
case MOTOR2_A:
case MOTOR2_B:
motorPWM = MOTOR2_PWM;
break;
case MOTOR3_A:
case MOTOR3_B:
motorPWM = MOTOR3_PWM;
break;
case MOTOR4_A:
case MOTOR4_B:
motorPWM = MOTOR4_PWM;
break;
default:
// Use speed as error flag, -3333 = invalid output.
speed = -3333;
break;
}
if (speed != -3333)
{
shiftWrite(output, high_low);
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPWM, speed);
}
}
}
void shiftWrite(int output, int high_low)
{
static int latch_copy;
static int shift_register_initialized = false;
if (!shift_register_initialized)
{
pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);
// Set pins for shift register to default value (low);
digitalWrite(MOTORDATA, LOW);
digitalWrite(MOTORLATCH, LOW);
digitalWrite(MOTORCLK, LOW);
// Enable the shift register, set Enable pin Low.
digitalWrite(MOTORENABLE, LOW);
// start with all outputs (of the shift register) low
latch_copy = 0;
shift_register_initialized = true;
}
// The defines HIGH and LOW are 1 and 0.
// So this is valid.
bitWrite(latch_copy, output, high_low);
// Use the default Arduino 'shiftOut()' function to
// shift the bits with the MOTORCLK as clock pulse.
// The 74HC595 shiftregister wants the MSB first.
// After that, generate a latch pulse with MOTORLATCH.
shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, HIGH);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, LOW);
}
void循环()
{
//处理任何多播DNS请求
mdns.update();
Adafruit_CC3000_ClientRef client=restServer.available();
休息、处理(客户);
wdt_重置();
如果(!cc3000.checkConnected()){while(1){}
wdt_重置();
}
int forward(字符串命令){
goforwards();
返回1;
}
向后int(字符串命令){
gobackwards();
返回1;
}
int left(字符串命令){
序列号。println(F(“向左…”);
//高尔夫();
返回1;
}
int right(字符串命令){
Serial.println(F(“向右…”);
//戈赖特();
返回1;
}
int estop(字符串命令){
Serial.println(F(“要求停止…”);
allstop();
返回1;
}
void goforwards(){
序列号println(F(“前进…”);
马达(1,向前,150);马达(4,向前,150);马达(2,向前,150);马达(3,向前,150);延迟(500);
allstop();
}
void gobackwards(){
Serial.println(F(“向后走…”);
电机(1,向后,150);电机(4,向后,150);电机(2,向后,150);电机(3,向后,150);延迟(500);
allstop();
}
void goleft(){motor(1,FORWARD,175);motor(4,FORWARD,175);motor(2,FORWARD,50);motor(3,FORWARD,50);delay(2000);}
void goright(){motor(1,FORWARD,50);motor(4,FORWARD,50);motor(2,FORWARD,175);motor(3,FORWARD,175);delay(2000);}
void allstop(){motor(1,RELEASE,0);motor(2,RELEASE,0);motor(3,RELEASE,0);motor(4,RELEASE,0);delay(1);}
无效电机(int nMotor、int命令、int速度)
{
int motorA,motorB;
如果(nMotor>=1&&nMotor=0&&speed,我建议您1)更好地格式化代码,尤其是缩进(否则几乎无法清晰地阅读)2)要确定代码挂起的位置。为了做到这一点,您可以在代码中添加一些Serial.println
s,并检查挂起的位置。我建议您1)对代码进行更好的格式化,尤其是缩进(否则几乎无法清晰地阅读)2)要确定挂起的点是什么。为此,您可以向代码中添加一些Serial.println
s,并检查挂起的位置。