【Curie Nano试用】 B1.项目-疯狂的四驱车
本帖最后由 limale 于 2017-5-22 21:18 编辑Curie Nano玩了也有段时间了,学习的过程中确实也遇到了一些问题好在都一一解决了。当初申请的时候就是看中它集成了蓝牙,所以当时想申请来做一个蓝牙小车,经过找资料逐步的学习,一个功能简单的蓝牙小车实现了。
下来介绍一下硬件构成:
1.四个12V的直流电机
2.四个万向轮
3.两个L298N电机驱动板模块
4.一块开好孔的亚克力板
5.12V电源(我这里是用三块锂电池级联)
6.Curie Nano肯定必不可少了
7.LCD屏幕(可选)
8.排针、杜邦线若干
装好了之后就是如下图的样子了。
LCD屏幕就显示了运动状态:前、后、左、右、停止。
软件部分主要是通过Nordic BLE UART Service来实现,Tx特征字发送一个字符来控制电机,具体的实现看代码吧。#include "oled.h"
#include "CurieIMU.h"
#include <CurieBLE.h>
//Nordic BLE UART Service
BLEService UART("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
//Tx Characterisitic
BLEUnsignedCharCharacteristic Tx("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLERead | BLEWrite);
//IN1 IN2 IN3 IN4
const int motorL1 = 2;
const int motorL2 = 3;
const int motorL3 = 9;
const int motorL4 = 10;
const int motorR1 = 11;
const int motorR2 = 12;
const int motorR3 = 13;
const int motorR4 = 20;
char led_on[] = {"LED on"};
char led_off[] = {"LED off"};
char forward[] = {"Forward"};
char backward[] = {"Backward"};
char left[] = {"Left "};
char right[] = {"Right "};
char stopit[] = {"Stop "};
void setup() {
// put your setup code here, to run once:
Sys_Init();
//// initialize device
//Serial.println("Initializing IMU device...");
//CurieIMU.begin();
//
//// Set the accelerometer range to 2G
//CurieIMU.setAccelerometerRange(2);
//
//// Set the accelerometer range to 250 degrees/second
//CurieIMU.setGyroRange(250);
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorL3, OUTPUT);
pinMode(motorL4, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
pinMode(motorR3, OUTPUT);
pinMode(motorR4, OUTPUT);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, LOW);
Serial.begin(9600);
// begin initialization
BLE.begin();
// set advertised local name and service UUID:
BLE.setLocalName("Arduino101_UART");
BLE.setAdvertisedService(UART);
// add service and characteristic:
// add the service
BLE.addService(UART);
// add the characteristics to the service
UART.addCharacteristic(Tx);
// set the initial value for the characeristic:
Tx.setValue(0);
// begin advertising BLE service:
// start advertising
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// put your main code here, to run repeatedly:
Main();
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral
if (central)
{
Serial.print("Connected to central: ");
Serial.println(central.address()); // print the central's MAC address
while (central.connected())
{
// if the remote device wrote to the characteristic,
if (Tx.written())
{
switch (Tx.value())
{
case '0':
digitalWrite(13, HIGH);
OLED_ShowString(32, 2, led_on, 16);
break;
case '1':
digitalWrite(13, LOW);
OLED_ShowString(32, 2, led_off, 16);
break;
case '2': // forward
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, HIGH);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, LOW);
OLED_ShowString(32, 2, forward, 16);
break;
case '3': // backward
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorL3, HIGH);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, LOW);
OLED_ShowString(32, 2, backward, 16);
break;
case '4': // left
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, HIGH);
OLED_ShowString(32, 2, left, 16);
break;
case '5': // right
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
digitalWrite(motorR3, HIGH);
digitalWrite(motorR4, LOW);
OLED_ShowString(32, 2, right, 16);
break;
case '6': // stopit
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, LOW);
OLED_ShowString(32, 2, stopit, 16);
break;
default:
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorL3, LOW);
digitalWrite(motorL4, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorR3, LOW);
digitalWrite(motorR4, LOW);
break;
}
delay(1);
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}目前实现的功能还比较简单,电机也没有用PWM来控制而是直接驱动所以速度也是不能改变的,有空了再搞吧。因为不会写手机app只能用官方的app nRF Connect来控制,好在该工具提供宏命令录制真是很方便,不然一个个点得累死。
如下图所示,我录制了需要的几个命令,点击三角箭头就可以发送指令了。
录制了一个小视屏看看效果:
http://player.youku.com/player.php/sid/XMjc3OTc1NzAwNA==/v.swf
工程和参考资料:
页:
[1]