# RemoteXY手机加速度传感器控制小车 **Repository Path**: jun-tian/RemoteXY ## Basic Information - **Project Name**: RemoteXY手机加速度传感器控制小车 - **Description**: 使用智能手机上的 加速度 传感器控制arduinouino 小车 - **Primary Language**: Arduino - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-02-16 - **Last Updated**: 2023-05-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: RemoteXY ## README # RemoteXY手机加速度传感器控制小车 #### 介绍 使用智能手机上的 加速度 传感器控制arduinouino 小车,使用此服务 RemoteXY 非常容易地设置一个远程控制机器人平台或两辆汽车。我们将使用控制元件"操纵杆"管理机器人,该操纵杆可以与智能手机的G传感器配合使用。 #### 主代码 ``` ///////////////////////////////////////////// // RemoteXY include library // ///////////////////////////////////////////// /* RemoteXY select connection mode and include library */ #define REMOTEXY_MODE__SOFTWARESERIAL #include #include /* RemoteXY connection settings */ #define REMOTEXY_SERIAL_RX 2 #define REMOTEXY_SERIAL_TX 3 #define REMOTEXY_SERIAL_SPEED 9600 /* RemoteXY configurate */ unsigned char RemoteXY_CONF[] = { 3,0,23,0,1,5,5,15,41,11 ,43,43,1,2,0,6,5,27,11,5 ,79,78,0,79,70,70,0 }; /* this structure defines all the variables of your control interface */ struct { /* input variable */ signed char joystick_1_x; /* =-100..100 x-coordinate joystick position */ signed char joystick_1_y; /* =-100..100 y-coordinate joystick position */ unsigned char switch_1; /* =1 if switch ON and =0 if OFF */ /* other variable */ unsigned char connect_flag; /* =1 if wire connected, else =0 */ } RemoteXY; ///////////////////////////////////////////// // END RemoteXY include // ///////////////////////////////////////////// /* defined the right motor control pins */ #define PIN_MOTOR_RIGHT_UP 7 #define PIN_MOTOR_RIGHT_DN 6 #define PIN_MOTOR_RIGHT_SPEED 10 /* defined the left motor control pins */ #define PIN_MOTOR_LEFT_UP 5 #define PIN_MOTOR_LEFT_DN 4 #define PIN_MOTOR_LEFT_SPEED 9 /* defined the LED pin */ #define PIN_LED 13 /* defined two arrays with a list of pins for each motor */ unsigned char RightMotor[3] = {PIN_MOTOR_RIGHT_UP, PIN_MOTOR_RIGHT_DN, PIN_MOTOR_RIGHT_SPEED}; unsigned char LeftMotor[3] = {PIN_MOTOR_LEFT_UP, PIN_MOTOR_LEFT_DN, PIN_MOTOR_LEFT_SPEED}; /* speed control of the motor motor - pointer to an array of pins v - motor speed can be set from -100 to 100 */ void Wheel (unsigned char * motor, int v) { if (v>100) v=100; if (v<-100) v=-100; if (v>0) { digitalWrite(motor[0], HIGH); digitalWrite(motor[1], LOW); analogWrite(motor[2], v*2.55); } else if (v<0) { digitalWrite(motor[0], LOW); digitalWrite(motor[1], HIGH); analogWrite(motor[2], (-v)*2.55); } else { digitalWrite(motor[0], LOW); digitalWrite(motor[1], LOW); analogWrite(motor[2], 0); } } void setup() { /* initialization pins */ pinMode (PIN_MOTOR_RIGHT_UP, OUTPUT); pinMode (PIN_MOTOR_RIGHT_DN, OUTPUT); pinMode (PIN_MOTOR_LEFT_UP, OUTPUT); pinMode (PIN_MOTOR_LEFT_DN, OUTPUT); pinMode (PIN_LED, OUTPUT); /* initialization module RemoteXY */ RemoteXY_Init (); } void loop() { /* event handler module RemoteXY */ RemoteXY_Handler (); /* manage LED pin */ digitalWrite (PIN_LED, (RemoteXY.switch_1==0)?LOW:HIGH); /* manage the right motor */ Wheel (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x); /* manage the left motor */ Wheel (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x); } ``` #### 安装教程 我们组装一个非常简单的两轮平台,向您展示如何构建远程控制系统。该平台由以下组件组成(我们不要求做工,该平台的组装是为了展示资源RemoteXY的功能): Сhassis - 我们从其片材中切除; 前轮 - 车轮360度旋转; 减速电机 2个; 轮毂,轴适用于减速电机2个。我们一起购买的车轮与减速电机; 电池盒,第4节AA型电池上有开关; Arduino,我们使用了所有相同的克隆Seeeduino; 蓝牙HC-06模块; 电机驱动芯片 L298N; 使用remotexy编辑器并构造以下接口控件: [编辑器](https://remotexy.com/en/editor/) 设置在屏幕中间的"操纵杆"。在操纵杆的属性中选择附加控制"启用G传感器"。选择开关位置 G 传感器"左下角"。相同的设置可以将颜色更改为红色。将来,我们将用操纵杆来控制机器的运动。 设置"开关"。将其放在操纵杆的左侧。您也可以更改其颜色。开关,我们将在引脚13处驱动Arduino板上的LED。 如果你做对了一切,你应该大致拥有管理界面: 机器人控制接口 在项目设置中,选择目标平台,我们获取其源代码:"Arduino(SoftwareSerial),库版本"。按下"获取代码"按钮,然后在计算机上下载源代码。由链接下载库 RemoteXY 并将其安装在 Arduino IDE 中。 在IDE Arduino中打开下载的草图。完美的草图编译没有错误。但是,管理我们的机器肯定不是代码。我们的任务是纠正此代码。为了正确起见,我们将使用下载的示例。 注意项目草图中结构 RemoteXY 的定义。该结构包含完全符合用户界面上已建立的控件的字段。我们看到变量joystick_1_x和joystick_1_y,反映了操纵杆的x和y坐标,以及反映开关的变量switch_1。 #### 使用说明 ![电路连接图](cir.png) #### 参与贡献