操纵杆控制的遥控车
组件和用品
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 |
关于这个项目
1. 开车!
首先你需要一辆旧的遥控车。幸运的是我找到了一辆怪物卡车。这可能非常有趣!
除控制两个电机的电缆外,拆下所有电子设备。
2. Arduino 和 Shield
拿一块 Arduino 板(我选择了一块 Arduino UNO)并将 USB 主机扩展板插在上面。
3. 将 Arduino 连接到汽车
将屏蔽罩插入 Arduino UNO 和 USB 主机屏蔽罩的顶部,然后将它们拧到汽车上。我使用了旧电子电路的一个孔。
4. 制作电路
我是在面包板上制作的,但您也可以在穿孔板上制作它。该电路本质上由两个驱动器(L293D 或 SN754410)和两个稳压器组成。稳压器用于限制来自电池的电压。事实上,我建议您使用 10-12 节 AA 1.5V 电池,以获得 15-18V 的最大电压,让您有更多时间玩遥控车。由于电机需要大量电流,为了将汽车从静止位置(约 2A)移动,我们需要一个强大的稳压器,如 78S12。
如果您想了解有关驱动程序如何工作的更多信息,请查看此处!
5. 将电路连接到汽车
在遥控车上找一个空的地方,你可以放置电路。在我拥有的巨型卡车上,有很多空间。定位后,将电路连接到两个电机。最后将无线接收器插入USB主机屏蔽。
6. 代码
该代码基于由来自circuits@home 的Kristian Lauszus 在GitHub 上托管的USB Host Library 2.0 示例
USB_Host_Shield_2.0/*Xbox 无线接收器库的示例草图 - 由 Kristian Lauszus 开发它支持最多四个无线控制器有关更多信息,请参阅博客文章:http://blog.tkjelectronics.dk/2012/12 /xbox-360-receiver- added-to-the-usb-host-library/ 或者给我发邮件:[email protected]*/#include //满足IDE,只需要看ino中的include语句。#ifdef dobogusinclude#include #endifUSB Usb;XBOXRECV Xbox(&Usb);#define motorPinOne 5 //选择的引脚必须有PWM#define motorPinTwo 6 //选择的引脚必须有PWM#define motor2PinOne 4 #define motor2PinTwo 7#define BRAKE 2/* 定义电机的转速。必须在 0 到 255 之间。 */int 脉冲 =0;int forward =0;int brac_on =0;void setup() { TCCR1B =TCCR1B &0b11111000 | 0x02; Serial.begin(115200); if (Usb.Init() ==-1) { Serial.print(F("\r\nOSC 没有启动"));同时(1); //停止motor_stop();中央(); } Serial.print(F("\r\nXbox 无线接收器库已启动")); pinMode (motorPinOne, OUTPUT); pinMode (motorPinTwo, OUTPUT); pinMode (motor2PinOne, OUTPUT); pinMode (motor2PinTwo, OUTPUT); pinMode(刹车,输出); digitalWrite (BRAKE, LOW);}void loop() { Usb.Task(); if(Xbox.XboxReceiverConnected) { for(uint8_t i=0;i<4;i++) { if(Xbox.getButtonPress(L2, i)) { Serial.print("L2:"); Serial.print(Xbox.getButtonPress(L2, i));脉冲 =Xbox.getButtonPress(L2, i);前进 =0;顺时针(); } if(Xbox.getButtonPress(R2, i)) { Serial.print("R2:"); Serial.println(Xbox.getButtonPress(R2, i));脉冲 =Xbox.getButtonPress(R2, i);前进 =1;逆时针(); } if(Xbox.getAnalogHat(LeftHatX, i)> 7500 || Xbox.getAnalogHat(LeftHatX, i) <-7500 || Xbox.getAnalogHat(LeftHatY, i)> 7500 || Xbox.getAnalogHat(LeftHatY, i) <- 7500 || Xbox.getAnalogHat(RightHatX, i)> 7500 || Xbox.getAnalogHat(RightHatX, i) <-7500 || Xbox.getAnalogHat(RightHatY, i)> 7500 || Xbox.getAnalogHat(RightHatY, i) <- 7500) { if(Xbox.getAnalogHat(LeftHatX, i)> 7500) { Serial.print(F("LeftHatX:")); Serial.print(Xbox.getAnalogHat(LeftHatX, i)); Serial.print("\t");如果 (Xbox.getAnalogHat(LeftHatX, i)> 26000) turn_right();否则中心(); } if (Xbox.getAnalogHat(LeftHatX, i) <-7500) { Serial.print(F("LeftHatX:")); Serial.print(Xbox.getAnalogHat(LeftHatX, i)); Serial.print("\t");如果 (Xbox.getAnalogHat(LeftHatX, i) <-26000) turn_left();否则中心(); } if(Xbox.getAnalogHat(LeftHatY, i)> 7500 || Xbox.getAnalogHat(LeftHatY, i) <-7500) { Serial.print(F("LeftHatY:")); Serial.print(Xbox.getAnalogHat(LeftHatY, i)); Serial.print("\t"); } if(Xbox.getAnalogHat(RightHatX, i)> 7500) { Serial.print(F("RightHatX:")); Serial.print(Xbox.getAnalogHat(RightHatX, i)); Serial.print("\t"); } if (Xbox.getAnalogHat(RightHatX, i) <-7500) { Serial.print(F("RightHatX:")); Serial.print(Xbox.getAnalogHat(RightHatX, i)); Serial.print("\t"); } if(Xbox.getAnalogHat(RightHatY, i)> 7500 || Xbox.getAnalogHat(RightHatY, i) <-7500) { Serial.print(F("RightHatY:")); Serial.print(Xbox.getAnalogHat(RightHatY, i)); Serial.println(); } if(Xbox.getButtonClick(UP, i)) { Serial.println(F("Up")); } if(Xbox.getButtonClick(DOWN, i)) { Serial.println(F("Down")); } if(Xbox.getButtonClick(LEFT, i)) { Serial.println(F("Left")); } if(Xbox.getButtonClick(RIGHT, i)) { Serial.println(F("Right")); } if(Xbox.getButtonClick(START, i)) { Serial.println(F("Start")); } if(Xbox.getButtonClick(BACK, i)) { Serial.println(F("Back")); } if(Xbox.getButtonClick(L3, i)) Serial.println(F("L3")); if(Xbox.getButtonClick(R3, i)) Serial.println(F("R3")); if(Xbox.getButtonClick(L1, i)) Serial.println(F("L1")); if(Xbox.getButtonClick(R1, i)) Serial.println(F("R1")); if(Xbox.getButtonClick(XBOX, i)) { Xbox.setLedMode(ROTATING, i); Serial.println(F("Xbox")); } if(Xbox.getButtonClick(A, i)) { Serial.println(F("A")); } if(Xbox.getButtonClick(B, i)) { Serial.println(F("B"));数字写入(刹车,高);刹车开 =1;脉冲 =100;如果(向前)顺时针();否则逆时针(); } if(Xbox.getButtonClick(X, i)) Serial.println(F("X")); if(Xbox.getButtonClick(Y, i)) { Serial.println(F("Y"));电机停止(); } } } delay(1);}void 顺时针(){ Serial.print("旋转是顺时针,速度是"); Serial.println(脉冲);模拟写入(motorPinOne,脉冲); // 将 H 桥的第一脚设置为低模拟Write(motorPinTwo,0);如果(brake_on){延迟(600);数字写入(刹车,低);刹车开 =0;脉冲 =0; }}void counterClockwise(){ Serial.print("旋转是逆时针,速度是"); Serial.println(脉冲);模拟写入(motorPinOne,0); // 将 H 桥的第一脚设置为低模拟Write(motorPinTwo,pulse);如果(brake_on){延迟(600);数字写入(刹车,低);刹车开 =0;脉冲 =0; }}void turn_left(){ digitalWrite(motor2PinOne,HIGH); // 将 H 桥的第一脚设置为低电平 digitalWrite(motor2PinTwo,LOW);}void turn_right(){ digitalWrite(motor2PinOne,LOW); // 将 H 桥的第一脚设置为低电平 digitalWrite(motor2PinTwo,HIGH);}void motor_stop(){analogWrite(motorPinOne,0);模拟写入(motorPinTwo,0);脉冲 =0;}void center(){ digitalWrite(motor2PinOne, LOW);数字写入(motor2PinTwo,低);}
代码
- 代码片段 #1
代码片段 #1Arduino
/* Xbox Wireless Reciver 库的示例草图 - 由 Kristian Lauszus 开发 它最多支持四个无线控制器 有关更多信息,请参阅博客文章:http://blog.tkjelectronics.dk/2012/12/xbox-360 -receiver-add-to-the-usb-host-library/ 或者给我发邮件:[email protected] */#include//满足IDE,只需要看include语句在 ino.#ifdef dobogusinclude#include #endifUSB Usb;XBOXRECV Xbox(&Usb);#define motorPinOne 5 //选择的管脚必须有 PWM#define motorPinTwo 6 //选择的管脚必须有 PWM#define motor2PinOne 4 #define motor2PinTwo 7#define BRAKE 2/* 定义电机的转速。必须在 0 到 255 之间。 */int 脉冲 =0;int forward =0;int brac_on =0;void setup() { TCCR1B =TCCR1B &0b11111000 | 0x02; Serial.begin(115200); if (Usb.Init() ==-1) { Serial.print(F("\r\nOSC 没有启动"));同时(1); //停止motor_stop();中央(); } Serial.print(F("\r\nXbox 无线接收器库已启动")); pinMode (motorPinOne, OUTPUT); pinMode (motorPinTwo, OUTPUT); pinMode (motor2PinOne, OUTPUT); pinMode (motor2PinTwo, OUTPUT); pinMode(刹车,输出); digitalWrite (BRAKE, LOW);}void loop() { Usb.Task(); if(Xbox.XboxReceiverConnected) { for(uint8_t i=0;i<4;i++) { if(Xbox.getButtonPress(L2, i)) { Serial.print("L2:"); Serial.print(Xbox.getButtonPress(L2, i));脉冲 =Xbox.getButtonPress(L2, i);前进 =0;顺时针(); } if(Xbox.getButtonPress(R2, i)) { Serial.print("R2:"); Serial.println(Xbox.getButtonPress(R2, i));脉冲 =Xbox.getButtonPress(R2, i);前进 =1;逆时针(); } if(Xbox.getAnalogHat(LeftHatX, i)> 7500 || Xbox.getAnalogHat(LeftHatX, i) <-7500 || Xbox.getAnalogHat(LeftHatY, i)> 7500 || Xbox.getAnalogHat(LeftHatY, i) <- 7500 || Xbox.getAnalogHat(RightHatX, i)> 7500 || Xbox.getAnalogHat(RightHatX, i) <-7500 || Xbox.getAnalogHat(RightHatY, i)> 7500 || Xbox.getAnalogHat(RightHatY, i) <- 7500) { if(Xbox.getAnalogHat(LeftHatX, i)> 7500) { Serial.print(F("LeftHatX:")); Serial.print(Xbox.getAnalogHat(LeftHatX, i)); Serial.print("\t");如果 (Xbox.getAnalogHat(LeftHatX, i)> 26000) turn_right();否则中心(); } if (Xbox.getAnalogHat(LeftHatX, i) <-7500) { Serial.print(F("LeftHatX:")); Serial.print(Xbox.getAnalogHat(LeftHatX, i)); Serial.print("\t");如果 (Xbox.getAnalogHat(LeftHatX, i) <-26000) turn_left();否则中心(); } if(Xbox.getAnalogHat(LeftHatY, i)> 7500 || Xbox.getAnalogHat(LeftHatY, i) <-7500) { Serial.print(F("LeftHatY:")); Serial.print(Xbox.getAnalogHat(LeftHatY, i)); Serial.print("\t"); } if(Xbox.getAnalogHat(RightHatX, i)> 7500) { Serial.print(F("RightHatX:")); Serial.print(Xbox.getAnalogHat(RightHatX, i)); Serial.print("\t"); } if (Xbox.getAnalogHat(RightHatX, i) <-7500) { Serial.print(F("RightHatX:")); Serial.print(Xbox.getAnalogHat(RightHatX, i)); Serial.print("\t"); } if(Xbox.getAnalogHat(RightHatY, i)> 7500 || Xbox.getAnalogHat(RightHatY, i) <-7500) { Serial.print(F("RightHatY:")); Serial.print(Xbox.getAnalogHat(RightHatY, i)); Serial.println(); } if(Xbox.getButtonClick(UP, i)) { Serial.println(F("Up")); } if(Xbox.getButtonClick(DOWN, i)) { Serial.println(F("Down")); } if(Xbox.getButtonClick(LEFT, i)) { Serial.println(F("Left")); } if(Xbox.getButtonClick(RIGHT, i)) { Serial.println(F("Right")); } if(Xbox.getButtonClick(START, i)) { Serial.println(F("Start")); } if(Xbox.getButtonClick(BACK, i)) { Serial.println(F("Back")); } if(Xbox.getButtonClick(L3, i)) Serial.println(F("L3")); if(Xbox.getButtonClick(R3, i)) Serial.println(F("R3")); if(Xbox.getButtonClick(L1, i)) Serial.println(F("L1")); if(Xbox.getButtonClick(R1, i)) Serial.println(F("R1")); if(Xbox.getButtonClick(XBOX, i)) { Xbox.setLedMode(ROTATING, i); Serial.println(F("Xbox")); } if(Xbox.getButtonClick(A, i)) { Serial.println(F("A")); } if(Xbox.getButtonClick(B, i)) { Serial.println(F("B"));数字写入(刹车,高);刹车开 =1;脉冲 =100;如果(向前)顺时针();否则逆时针(); } if(Xbox.getButtonClick(X, i)) Serial.println(F("X")); if(Xbox.getButtonClick(Y, i)) { Serial.println(F("Y"));电机停止(); } } } delay(1);}void 顺时针(){ Serial.print("旋转是顺时针,速度是"); Serial.println(脉冲);模拟写入(motorPinOne,脉冲); // 将 H 桥的第一脚设置为低模拟Write(motorPinTwo,0);如果(brake_on){延迟(600);数字写入(刹车,低);刹车开 =0;脉冲 =0; }}void counterClockwise(){ Serial.print("旋转是逆时针,速度是"); Serial.println(脉冲);模拟写入(motorPinOne,0); // 将 H 桥的第一脚设置为低模拟Write(motorPinTwo,pulse);如果(brake_on){延迟(600);数字写入(刹车,低);刹车开 =0;脉冲 =0; } }void turn_left(){ digitalWrite(motor2PinOne,HIGH); // 将 H 桥的第一脚设置为低电平 digitalWrite(motor2PinTwo,LOW);}void turn_right(){ digitalWrite(motor2PinOne,LOW); // 将 H 桥的第一脚设置为低电平 digitalWrite(motor2PinTwo,HIGH);}void motor_stop(){analogWrite(motorPinOne,0);模拟写入(motorPinTwo,0);脉冲 =0;}void center(){ digitalWrite(motor2PinOne, LOW);数字写入(motor2PinTwo,低);}
Github
https://github.com/felis/USB_Host_Shield_2.0https://github.com/felis/USB_Host_Shield_2.0制造工艺