【智能无线小车系列一】小车电机运动测试程序

下面的代码演示的是让小车直线前进的arduino程序:

/*the following codes functions to make the car run forward repeatedly*/

int pin1=8;
int pin2=9;
int speedpin1=11;
int pin3=6;
int pin4=7;
int speedpin2=10;

void setup() {
  // put your setup code here, to run once:
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  pinMode(speedpin1,OUTPUT);
  pinMode(pin3,OUTPUT);
  pinMode(pin4,OUTPUT);
  pinMode(speedpin2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
      analogWrite(speedpin1,150);//set the PWM speed as 100,range from 0 to 250
      analogWrite(speedpin2,150);//set the PWM speed as 100
      digitalWrite(pin1,LOW);
      digitalWrite(pin2,HIGH);
      digitalWrite(pin3,HIGH);
      digitalWrite(pin4,LOW);
      delay(2000);    

}

下面的代码演示的通过树莓派传输控制指令控制小车基本运动:按键8:——前进,按键2——后退,按键4——左转弯,按键6——右转弯,按键5——停止。

int pin1=8;
int pin2=9;
int speedpin1=11;
int pin3=6;
int pin4=7;
int speedpin2=10;
char sign;

void setup() {
  // put your setup code here, to run once:
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  pinMode(speedpin1,OUTPUT);
  pinMode(pin3,OUTPUT);
  pinMode(pin4,OUTPUT);
  pinMode(speedpin2,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

    if(Serial.available())
 {
   sign=Serial.read();
   switch (sign){  

   case ‘4‘://backword
   {
      analogWrite(speedpin1,200);//set the PWM speed as 100
      analogWrite(speedpin2,200);//set the PWM speed as 100
      digitalWrite(pin1,HIGH);//set the motor run right for 2 seconds
      digitalWrite(pin2,LOW);
      digitalWrite(pin3,HIGH);
      digitalWrite(pin4,LOW);    

      break;

   }

   case ‘8‘://forward
    {
      analogWrite(speedpin1,200);//set the PWM speed as 100
      analogWrite(speedpin2,200);//set the PWM speed as 100
      digitalWrite(pin1,LOW);//set the motor run right for 2 seconds
      digitalWrite(pin2,HIGH);
      digitalWrite(pin3,HIGH);
      digitalWrite(pin4,LOW);    

      break;
    }

     case ‘6‘://right
   {
      analogWrite(speedpin1,200);//set the PWM speed as 100
      analogWrite(speedpin2,200);//set the PWM speed as 100
      digitalWrite(pin1,LOW);//set the motor run right for 2 seconds
      digitalWrite(pin2,HIGH);
      digitalWrite(pin3,LOW);
      digitalWrite(pin4,HIGH);      

      break;
   }

     case ‘2‘://left
   {
      analogWrite(speedpin1,200);//set the PWM speed as 100
      analogWrite(speedpin2,200);//set the PWM speed as 100
      digitalWrite(pin1,HIGH);//set the motor run right for 2 seconds
      digitalWrite(pin2,LOW);
      digitalWrite(pin3,LOW);
      digitalWrite(pin4,HIGH);

      break;
   }

     case ‘5‘://stop
   {
     analogWrite(speedpin1,0);//set the PWM speed as 0
     analogWrite(speedpin2,0);//set the PWM speed as 0
     break;
   }
   Serial.flush();
   }
 }

}
时间: 2024-08-02 15:14:15

【智能无线小车系列一】小车电机运动测试程序的相关文章

第二天:SLAM智能小车DIY乐趣-小车控制stm32软件基础

SLAM智能小车DIY乐趣-小车控制stm32软件基础 ####写在前面#### 前面介绍了小车控制stm32硬件基础,本文就来介绍配套的小车控制stm32软件基础.关于stm32开发相关的基础知识这里就不多说了,有需要的小伙伴可以查阅相关资料进行学习,这里重点从小车控制项目入手,直接进行跟实际需求相关的开发.本文主要内容: 1) 电机控制 2) 编码器数据读取 3) 串口数据收发 4) 电机速度PID控制 5) 周期性控制 6) 小车控制软件整体框图 ####正文#### 1.电机控制 电机控

智能无线交通信号控制系统方案

前言   随着城市车辆数目的急剧增长,车辆与路面的矛盾越来越突出,城市交通拥挤和堵塞导致道路的容量和管理效率降低 ,也是交通污染和事故的间接因素.尽管造成这种现象的原因很多 ,但各交叉口交通信号的不协调.不优化是主要的技术原因.由此而产生的城市智能交通优化交通信号系统将在很大程度上解决这一问题. 原有的交通信号控制系统采用光纤接入通信方式,施工困难,成本高,受地形.气候影响较大,设备维护困难:改用3G/4G网络,灵活性高,实现在线保持机制,3G/4G路由器网络速率已经满足信号机数据传输,通过运营

智能路由器-OpenWRT 系列六 (远程迅雷离线下载)

OpenWRT 系列教程 智能路由器-OpenWRT 系列一 (OpenWRT 刷机) 智能路由器-OpenWRT 系列二 (OpenWRT初始配置) 智能路由器-OpenWRT 系列三 (OpenWRT安装LuCI&网络配置) 智能路由器-OpenWRT 系列四 (挂载移动设备) 智能路由器-OpenWRT 系列五 (NAS-SMB家庭共享) 智能路由器-OpenWRT 系列六 (远程迅雷离线下载) 下载 Xware1.0.31,参考地址 http://luyou.xunlei.com/thr

BootStrap 智能表单系列 十 自动完成组件的支持

web开发中,肯定遇到像百度.google这种搜索的功能吧,那智能表单中的自动完成可以做什么呢,下面来揭晓: 1.包含像google.百度等类似的简单搜索 2.复杂结构的支持,比如说 输入产品编号,需要将产品的编号.产品的名称.产品的单价.产品的备注信息等填写会表单中的某个位置 代码如下(页面地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/master/demo/form3-ele-autocomplete.html): 自动完成

BootStrap 智能表单系列 十一 级联下拉的支持

像省市县选择的这种,但凡是个人肯定都见过,实现方式有很多种 1.有在第一级选择的时候去加载或者从本地对象中拿第一级对应的数据源显示到列表中,第二级以此类推 2.也有将所有的项都加载到select中,然后通过关联来显示或隐藏与上一级别无关的项 感觉还是第二种简单一些,于是使用了第二种方式来实现的,其余的就不废话了 代码如下(页面地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/master/demo/form3-ele-cascade

BootStrap 智能表单系列 九 表单图片上传的支持

本章介绍如何在生成表单后,可以支持上传图片后可以及时预览图片 代码如下(连接地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/master/demo/form3-ele-img.html): 依赖jquery-file-upload插件,需要引用jquery.ui.widget.js.jquery.iframe-transport.js.jquery.fileload.js 调用global.Fn.InitPlugin('img',

智能无线力/液位/温湿度传感器在智慧社压区中的应用

智慧社区包括智慧物业管理.电子商务服务.智慧养老服务.智慧家居等智慧社区是指充分利用物联网.云计算.移动互联网等新一代信息技术的集成应用.那么智能无线压力/液位/温湿度传感器,究竟是如何运用在智慧社区中的呢?今天我们所介绍的是智慧社区中的智慧物业管理,为社区居民提供一个安全.舒适.便利的现代化.智慧化生活环境,从而形成基于信息化.智能化社会管理与服务的一种新的管理形态的社区,是智慧物业管理的核心宗旨.为了实现这一目的,需要将物业旗下所属小区管网的管道水压.地库集水井水池水位,电梯地坑水浸水位探测

【智能无线小车系列二】组装小车

由于采购的小车来自于奥松机器人的AS-4WD铝合金移动机器人平台,主要由于其具有较高的越野型.机动性. 该平台的主要特点如下: 1.全铝合金车体,结实耐用.2.四个马达12V 电压独立驱动,动力强劲.3.结构合理,便于扩展.4.平台轮胎采用优质橡胶,具有减震.耐磨.抓地力强等特点,能同时适应光滑路面和崎岖路面环境. 车体整体外观如下: 话不多说,抓紧时间开始动手安装: 1.首先安装直流电机,每个电机需要两颗螺丝固定.固定完成之后,每个电机上连接两根导线,并用焊锡加以固定,套上热塑管,使得绝缘处绝

【智能无线小车系列一】物品采购

正所谓,“工欲善其事,必先利器”,在完成我们所要实现的小车之前,需要先到网上采购基本的组装器件.以下是一辆完整的小车的采购列表,仅供参考: