查看: 4685|回复: 0

【Curie Nano试用】 B1.项目-疯狂的四驱车

[复制链接]
  • TA的每日心情
    开心
    2024-3-3 21:23
  • 签到天数: 2449 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2017-5-22 21:14:48 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 limale 于 2017-5-22 21:18 编辑

    Curie Nano玩了也有段时间了,学习的过程中确实也遇到了一些问题好在都一一解决了。当初申请的时候就是看中它集成了蓝牙,所以当时想申请来做一个蓝牙小车,经过找资料逐步的学习,一个功能简单的蓝牙小车实现了。
    下来介绍一下硬件构成:
    1.四个12V的直流电机
    2.四个万向轮
    3.两个L298N电机驱动板模块
    4.一块开好孔的亚克力板
    5.12V电源(我这里是用三块锂电池级联)
    6.Curie Nano肯定必不可少了
    7.LCD屏幕(可选)
    8.排针、杜邦线若干


    装好了之后就是如下图的样子了。

    IMG_20170522_202137.jpg

    LCD屏幕就显示了运动状态:前、后、左、右、停止。
    IMG_20170522_202145.jpg

    软件部分主要是通过Nordic BLE UART Service来实现,Tx特征字发送一个字符来控制电机,具体的实现看代码吧。
    1. #include "oled.h"
    2. #include "CurieIMU.h"

    3. #include <CurieBLE.h>

    4. //Nordic BLE UART Service
    5. BLEService UART("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");

    6. //Tx Characterisitic
    7. BLEUnsignedCharCharacteristic Tx("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLERead | BLEWrite);
    8. //IN1 IN2 IN3 IN4
    9. const int motorL1 = 2;
    10. const int motorL2 = 3;
    11. const int motorL3 = 9;
    12. const int motorL4 = 10;

    13. const int motorR1 = 11;
    14. const int motorR2 = 12;
    15. const int motorR3 = 13;
    16. const int motorR4 = 20;

    17. char led_on[] = {"LED on"};
    18. char led_off[] = {"LED off"};

    19. char forward[] = {"Forward"};
    20. char backward[] = {"Backward"};
    21. char left[] = {"Left    "};
    22. char right[] = {"Right   "};
    23. char stopit[] = {"Stop    "};

    24. void setup() {
    25.   // put your setup code here, to run once:
    26.   Sys_Init();

    27.   //  // initialize device
    28.   //  Serial.println("Initializing IMU device...");
    29.   //  CurieIMU.begin();
    30.   //
    31.   //  // Set the accelerometer range to 2G
    32.   //  CurieIMU.setAccelerometerRange(2);
    33.   //
    34.   //  // Set the accelerometer range to 250 degrees/second
    35.   //  CurieIMU.setGyroRange(250);
    36.   pinMode(motorL1, OUTPUT);
    37.   pinMode(motorL2, OUTPUT);
    38.   pinMode(motorL3, OUTPUT);
    39.   pinMode(motorL4, OUTPUT);

    40.   pinMode(motorR1, OUTPUT);
    41.   pinMode(motorR2, OUTPUT);
    42.   pinMode(motorR3, OUTPUT);
    43.   pinMode(motorR4, OUTPUT);

    44.   digitalWrite(motorL1, LOW);
    45.   digitalWrite(motorL2, LOW);
    46.   digitalWrite(motorL3, LOW);
    47.   digitalWrite(motorL4, LOW);

    48.   digitalWrite(motorR1, LOW);
    49.   digitalWrite(motorR2, LOW);
    50.   digitalWrite(motorR3, LOW);
    51.   digitalWrite(motorR4, LOW);

    52.   Serial.begin(9600);

    53.   // begin initialization
    54.   BLE.begin();

    55.   // set advertised local name and service UUID:
    56.   BLE.setLocalName("Arduino101_UART");
    57.   BLE.setAdvertisedService(UART);

    58.   // add service and characteristic:
    59.   // add the service
    60.   BLE.addService(UART);

    61.   // add the characteristics to the service
    62.   UART.addCharacteristic(Tx);

    63.   // set the initial value for the characeristic:
    64.   Tx.setValue(0);

    65.   // begin advertising BLE service:
    66.   // start advertising
    67.   BLE.advertise();

    68.   Serial.println("Bluetooth device active, waiting for connections...");

    69. }

    70. void loop() {
    71.   // put your main code here, to run repeatedly:
    72.   Main();
    73.   // listen for BLE peripherals to connect:
    74.   BLEDevice central = BLE.central();

    75.   // if a central is connected to peripheral
    76.   if (central)
    77.   {
    78.     Serial.print("Connected to central: ");
    79.     Serial.println(central.address()); // print the central's MAC address

    80.     while (central.connected())
    81.     {
    82.       // if the remote device wrote to the characteristic,
    83.       if (Tx.written())
    84.       {
    85.         switch (Tx.value())
    86.         {
    87.           case '0':
    88.             digitalWrite(13, HIGH);
    89.             OLED_ShowString(32, 2, led_on, 16);
    90.             break;

    91.           case '1':
    92.             digitalWrite(13, LOW);
    93.             OLED_ShowString(32, 2, led_off, 16);
    94.             break;

    95.           case '2': // forward
    96.             digitalWrite(motorL1, HIGH);
    97.             digitalWrite(motorL2, LOW);
    98.             digitalWrite(motorL3, LOW);
    99.             digitalWrite(motorL4, HIGH);
    100.             digitalWrite(motorR1, LOW);
    101.             digitalWrite(motorR2, LOW);
    102.             digitalWrite(motorR3, LOW);
    103.             digitalWrite(motorR4, LOW);
    104.             OLED_ShowString(32, 2, forward, 16);
    105.             break;
    106.           case '3': // backward
    107.             digitalWrite(motorL1, LOW);
    108.             digitalWrite(motorL2, HIGH);
    109.             digitalWrite(motorL3, HIGH);
    110.             digitalWrite(motorL4, LOW);
    111.             digitalWrite(motorR1, LOW);
    112.             digitalWrite(motorR2, LOW);
    113.             digitalWrite(motorR3, LOW);
    114.             digitalWrite(motorR4, LOW);
    115.             OLED_ShowString(32, 2, backward, 16);
    116.             break;
    117.           case '4': // left
    118.             digitalWrite(motorL1, LOW);
    119.             digitalWrite(motorL2, LOW);
    120.             digitalWrite(motorL3, LOW);
    121.             digitalWrite(motorL4, LOW);
    122.             digitalWrite(motorR1, HIGH);
    123.             digitalWrite(motorR2, LOW);
    124.             digitalWrite(motorR3, LOW);
    125.             digitalWrite(motorR4, HIGH);
    126.             OLED_ShowString(32, 2, left, 16);
    127.             break;
    128.           case '5': // right
    129.             digitalWrite(motorL1, LOW);
    130.             digitalWrite(motorL2, LOW);
    131.             digitalWrite(motorL3, LOW);
    132.             digitalWrite(motorL4, LOW);
    133.             digitalWrite(motorR1, LOW);
    134.             digitalWrite(motorR2, HIGH);
    135.             digitalWrite(motorR3, HIGH);
    136.             digitalWrite(motorR4, LOW);
    137.             OLED_ShowString(32, 2, right, 16);
    138.             break;
    139.           case '6': // stopit
    140.             digitalWrite(motorL1, LOW);
    141.             digitalWrite(motorL2, LOW);
    142.             digitalWrite(motorL3, LOW);
    143.             digitalWrite(motorL4, LOW);
    144.             digitalWrite(motorR1, LOW);
    145.             digitalWrite(motorR2, LOW);
    146.             digitalWrite(motorR3, LOW);
    147.             digitalWrite(motorR4, LOW);
    148.             OLED_ShowString(32, 2, stopit, 16);
    149.             break;
    150.           default:
    151.             digitalWrite(motorL1, LOW);
    152.             digitalWrite(motorL2, LOW);
    153.             digitalWrite(motorL3, LOW);
    154.             digitalWrite(motorL4, LOW);
    155.             digitalWrite(motorR1, LOW);
    156.             digitalWrite(motorR2, LOW);
    157.             digitalWrite(motorR3, LOW);
    158.             digitalWrite(motorR4, LOW);
    159.             break;
    160.         }
    161.         delay(1);
    162.       }
    163.     }
    164.     Serial.print("Disconnected from central: ");
    165.     Serial.println(central.address());
    166.   }
    167. }
    复制代码
    目前实现的功能还比较简单,电机也没有用PWM来控制而是直接驱动所以速度也是不能改变的,有空了再搞吧。因为不会写手机app只能用官方的app nRF Connect来控制,好在该工具提供宏命令录制真是很方便,不然一个个点得累死。
    如下图所示,我录制了需要的几个命令,点击三角箭头就可以发送指令了。

    Screenshot_2017-05-22-21-04-00.png


    录制了一个小视屏看看效果:
    http://player.youku.com/player.php/sid/XMjc3OTc1NzAwNA==/v.swf
    工程和参考资料:
    car.zip (7.73 KB, 下载次数: 3)
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 04:08 , Processed in 0.112762 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.