极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13877|回复: 0

Processing 控制 H 桥电机运行

[复制链接]
发表于 2019-4-3 17:56:09 | 显示全部楼层 |阅读模式
Processing 控制 Arduino H-桥 两个电机运行



Processing 代码:

import processing.serial.*;
PFont font;
String ComX[] = null;
String SerialName = "COM1";
boolean serial;
int x1=50;
int y1=0;
int dp1=0;
int dp2=0;
int value=10;
int value1=10;
String mesg1="";
String mesg2="";
String mesg="";
int fx=1;
int sj=100;
PImage img1;
PImage img2;

void setup(){
size(800, 600);
ComX = myPort.list();
font = createFont("宋体.vlw",48);
textFont(font);
frameRate(30);
smooth();
printArray(ComX);

img1 = loadImage("dj.jpg");
img2 = loadImage("dj.jpg");
}
void draw(){
background(255);
drawWindows(20,50);
Serial_Handle(20,50);
CheckBoxHandle();
CheckBoxHandle_S();
CheckBoxHandle_PN();
}
void drawWindows(float x, float y){

drawForm(x,y);

if(button(x + 350, y + 450,"选串口")!= 0){
Serial_SetClick();
}
if(button(x + 500, y + 450,"发送指令") != 0){
  if(SerialName==null){
  }else{
send_data();
  }
}
if(button(x + 650, y + 450,"退  出") != 0){
exit();
}
}

//绘制主窗口
void drawForm(float x, float y){
strokeWeight(2);
textSize(18);
text("Processing-Arduino H-桥 两直流电机操控", 200,22);
fill(0);
strokeWeight(1);
textSize(12);
text("( 探索软件制 @ CopyRight 2019 )", 560,23);
textSize(18);
//strokeWeight(1);
fill(0);
line(0,25,1000,25);
line(0,300,1000,300);
line(0,485,1000,485);
image(img1, 5,40,80,80);
image(img2, 5,200,80,80);

if(dp1>0){
text("电机-1启动",200+x1,350+y1);
}else{
text("电机-1停止",200+x1,350+y1);
}

if(dp2>0){
text("电机-2启动",400+x1,350+y1);
}else{
text("电机-2停止",400+x1,350+y1);
}

textSize(18);
text("电机-1",50+x1,65+y1);
text("电机-2",50+x1,220+y1);
text("dp1=",350+x1,65+y1);
text(int(dp1),420+x1,65+y1);
text("dp2=",350+x1,220+y1);
text(int(dp2),420+x1,220+y1);
mesg="dp1="+dp1+","+"dp2="+dp2+","+"fx="+fx+","+"sj="+sj;
fill(237,28,36);
text(mesg1,280+x1,65+y1);
text(mesg2,280+x1,220+y1);
textSize(16);
strokeWeight(2);
fill(0);
text("向Arduino发送数据:",80,580);
text(mesg,240,580);
text("电平1增量选择=", 70, 120);
text(value, 185, 120);
text("电平2增量选择=", 70, 280);
text(value1, 185, 280);

text("方向:", 100, 465);
if(fx==1){
text("正转", 135, 465);
}else if(fx==2){
  text("反转", 135, 465);
}else{
text("停止", 135, 465);
}

text("当前串口=", 20, 520);
text(SerialName + "    9600bps", 100,520);

if(button(110+x1, 45+y1,"增加(+)") != 0){
mesg1="增加";
dp1=dp1+value;
dp1=constrain(dp1,0,255);
}
if(button(215+x1, 45+y1,"减少(-)") != 0){
mesg1="减少";
dp1=dp1-value;
dp1=constrain(dp1,0,255);
}
if(button(545+x1, 45+y1,"归零(0)") != 0){
mesg1="归零";
dp1=0;
dp1=constrain(dp1,0,255);
}

if(button(110+x1, 200+y1,"增加(+)") != 0){
mesg2="增加";
dp2=dp2+value1;
dp2=constrain(dp2,0,255);
}
if(button(215+x1, 200+y1,"减少(-)") != 0){
mesg2="减少";
dp2=dp2-value1;
dp2=constrain(dp2,0,255);
}
if(button(545+x1, 200+y1,"归零(0)") != 0){
mesg2="归零";
dp2=0;
dp2=constrain(dp2,0,255);
}
}

byte button(float x , float y , String str){
byte ret = 0;
//fill(0,255,0);
fill(0,0,0);
strokeWeight(2);
//stroke(255,255,0);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 100) ) &&(mouseY < (y + 30))){
fill(128,255,255);
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
fill(0,128,128);
ret = 1;
}
}
rect(x,y, 100, 30);
fill(255,255,255);
textSize(16);
text(str, x + 24, y + 22);
return ret;
}

//电平选择
boolean X_checkBox = true;
boolean Y_checkBox = false;
boolean Z_checkBox = false;
boolean A_checkBox = false;
boolean B_checkBox = false;
boolean C_checkBox = false;
boolean D_checkBox = false;
boolean E_checkBox = false;

void CheckBoxHandle(){
if(CheckBox(240,100,X_checkBox, "10")== 1){
X_checkBox = (X_checkBox == false) ? true: false;
value=10;
}
if(CheckBox(300,100,Y_checkBox, "20") == 1){
Y_checkBox = (Y_checkBox == false) ? true: false;
value=20;
}
if(CheckBox(360,100, Z_checkBox, "30") == 1){
Z_checkBox = (Z_checkBox == false) ? true: false;
value=30;
}
if(CheckBox(420,100, A_checkBox, "40") == 1){
A_checkBox = (A_checkBox == false) ? true: false;
value=40;
}
if(CheckBox(480,100, B_checkBox, "50") == 1){
B_checkBox = (B_checkBox == false) ? true: false;
value=50;
}
if(CheckBox(540,100, C_checkBox, "60") == 1){
C_checkBox = (C_checkBox == false) ? true: false;
value=60;
}
if(CheckBox(600,100, D_checkBox, "70") == 1){
D_checkBox = (D_checkBox == false) ? true: false;
value=70;
}
if(CheckBox(660,100, E_checkBox, "80") == 1){
E_checkBox = (E_checkBox == false) ? true: false;
value=80;
}
}
boolean checkBox_GetXFlag(){
return X_checkBox;
}
boolean checkBox_GetYFlag(){
return Y_checkBox;
}
boolean checkBox_GetZFlag(){
return Z_checkBox;
}
boolean checkBox_GetAFlag(){
return A_checkBox;
}
boolean checkBox_GetBFlag(){
return B_checkBox;
}
boolean checkBox_GetCFlag(){
return C_checkBox;
}
boolean checkBox_GetDFlag(){
return D_checkBox;
}
boolean checkBox_GetEFlag(){
return E_checkBox;
}


byte CheckBox(float x, float y, boolean sL, String str){
noFill();
//stroke(255,255,0);
if(sL){
fill(0,255,0);
}
rect(x, y, 20, 20);
textSize(16);
fill(0);
strokeWeight(2);
text(str, x + 30, y + 20);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 20) ) &&(mouseY < (y + 20))){
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
return 1;
}
}
return 0;
}
//步进电机步数选择
boolean S1_checkBox = true;
boolean S2_checkBox = false;
boolean S3_checkBox = false;
boolean S4_checkBox = false;
boolean S5_checkBox = false;
boolean S6_checkBox = false;
boolean S7_checkBox = false;
boolean S8_checkBox = false;
void CheckBoxHandle_S(){
if(CheckBox_S(240,260,S1_checkBox, "10")== 1){
S1_checkBox = (S1_checkBox == false) ? true: false;
value1=10;
}
if(CheckBox_S(300,260,S2_checkBox, "20") == 1){
S2_checkBox = (S2_checkBox == false) ? true: false;
value1=20;
}
if(CheckBox_S(360,260, S3_checkBox, "30") == 1){
S3_checkBox = (S3_checkBox == false) ? true: false;
value1=30;
}
if(CheckBox_S(420,260, S4_checkBox, "40") == 1){
S4_checkBox = (S4_checkBox == false) ? true: false;
value1=40;
}
if(CheckBox_S(480,260, S5_checkBox, "50") == 1){
S5_checkBox = (S5_checkBox == false) ? true: false;
value1=50;
}
if(CheckBox_S(540,260, S6_checkBox, "60") == 1){
S6_checkBox = (S6_checkBox == false) ? true: false;
value1=60;
}
if(CheckBox_S(600,260, S7_checkBox, "70") == 1){
S7_checkBox = (S7_checkBox == false) ? true: false;
value1=70;
}
if(CheckBox_S(660,260, S8_checkBox, "80") == 1){
S8_checkBox = (S8_checkBox == false) ? true: false;
value1=80;
}
}
boolean checkBox_GetS1Flag(){
return S1_checkBox;
}
boolean checkBox_GetS2Flag(){
return S2_checkBox;
}
boolean checkBox_GetS3Flag(){
return S3_checkBox;
}
boolean checkBox_GetS4Flag(){
return S4_checkBox;
}
boolean checkBox_GetS5Flag(){
return S5_checkBox;
}
boolean checkBox_GetS6Flag(){
return S6_checkBox;
}
boolean checkBox_GetS7Flag(){
return S7_checkBox;
}
boolean checkBox_GetS8Flag(){
return S8_checkBox;
}
byte CheckBox_S(float x, float y, boolean sL, String str){
noFill();
//stroke(255,255,0);
if(sL){
fill(0,255,0);
}
rect(x, y, 20, 20);
textSize(16);
fill(0);
strokeWeight(2);
text(str, x + 30, y + 20);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 20) ) &&(mouseY < (y + 20))){
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
return 1;
}
}
return 0;
}

//步进电机正反转选择
boolean P_checkBox = true;
boolean N_checkBox = false;
boolean S_checkBox = false;

void CheckBoxHandle_PN(){
if(CheckBox_P(240,450,P_checkBox, "正")== 1){
P_checkBox = (P_checkBox == false) ? true: false;
fx=1;
}
if(CheckBox_P(300,450,N_checkBox, "反") == 1){
N_checkBox = (N_checkBox == false) ? true: false;
fx=2;
}
if(CheckBox_P(360,450,S_checkBox, "停") == 1){
S_checkBox = (S_checkBox == false) ? true: false;
fx=3;
}
}
boolean checkBox_GetPFlag(){
return P_checkBox;
}
boolean checkBox_GetNFlag(){
return N_checkBox;
}
boolean checkBox_GetSFlag(){
return S_checkBox;
}

byte CheckBox_P(float x, float y, boolean sL, String str){
noFill();
//stroke(255,255,0);
if(sL){
fill(0,255,0);
}
rect(x, y, 20, 20);
textSize(16);
fill(0);
strokeWeight(2);
text(str, x + 30, y + 20);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 20) ) &&(mouseY < (y + 20))){
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
return 1;
}
}
return 0;
}

int name = 0;
String strName = String.valueOf(name);
byte click = 0;
byte comNum = 0;
byte Serial_StopFlag = 0;
boolean Serial_OpenFlag = false;
boolean Serial_DatareadFlag = false;
byte[] Serial_buffer = new byte[6];
Serial myPort;
String strSerial = "com1";
boolean Serial_GetdataFlag = false;
String[] strComBuff = new String[4];


void Serial_Handle(float x, float y){
if(click != 0){
comNum = 0;
Serial_DrawComX(20, 50);
}
else{
textSize(12);
//fill(255);
fill(0);
}
Serial_GetdataFromCom();
}

void Serial_DrawComX(float x, float y){
int i = 0;
int len = ComX.length;
textSize(12);
while(i < len){
if(comNum != 0){//选择了端口
break;
}
Serial_DrawCom(x + 350, y + 480 + i * 30, ComX);
i++;
}
}
void Serial_DrawCom(float x, float y, String str){
fill(255);
//noStroke();
rect(x, y, 101, 30);
if(mouseX > x && mouseY > y && mouseX < x + 100 && mouseY < y + 30){
fill(175,183,203);
rect(x,y, 101, 30);
if (mousePressed && (mouseButton == LEFT)) { //按键按下
if(str.equals("COM1") == true){
comNum = 1;
}
else if(str.equals("COM2") == true){
comNum = 1;
}
else if(str.equals("COM3") == true){
comNum = 1;
}
else if(str.equals("COM4") == true){
comNum = 1;
}
else if(str.equals("COM5") == true){
comNum = 1;
}
else if(str.equals("COM6") == true){
comNum = 1;
}
else if(str.equals("COM7") == true){
comNum = 1;
}
else if(str.equals("COM8") == true){
comNum = 1;
}
else if(str.equals("COM9") == true){
comNum = 1;
}
if(str.equals(SerialName) != true){
SerialName = str;
if(comNum != 0){
click = 0;
if(Serial_StopFlag != 0){
myPort.stop();//停止上一个串口
}
myPort =new Serial(this,SerialName,9600 );
myPort.bufferUntil(10);
Serial_StopFlag = 1;
Serial_OpenFlag = true;
}
}
click = 0;
}
}
fill(0,0,255);
text(str,x + 10, y + 25);
}

void Serial_SetClick(){
click = 1;
}
void Serial_GetdataFromCom(){
if(Serial_OpenFlag != true){
strComBuff[0] = " ";
strComBuff[1] = " ";
strComBuff[2] = " ";
return;
}
if(myPort.available() > 0){
Serial_GetdataFlag = true;
strSerial = myPort.readString();
strComBuff = strSerial.split(",");
}
}

float Serial_String2float(String str){
float a = 0;
a = float(str)*150;
return a;
}

boolean Serial_GetdataFlag(){
return Serial_GetdataFlag;
}

boolean Serial_GetComOpenFlag(){
return Serial_OpenFlag;
}

void serialEvent(Serial p) {
Serial_GetdataFlag = true;
strSerial = p.readString();
strComBuff = strSerial.split(",");
}

//send data to Arduino
void send_data(){
print("dp1=");
print(dp1);
print(',');
print("dp2=");
print(dp2);
print(',');
print("fx=");
print(fx);
print(',');
print("sj=");
println(sj);

myPort.write('%');
myPort.write(int(dp1));
myPort.write(int(dp2));
myPort.write(int(fx));
myPort.write(int(sj));
serial=false;
fill(0);
ellipse(400, 400, 30, 30);
}
boolean mousePressedFlag = false;
void mousePressed() {
mousePressedFlag = true;
}
boolean SaveFlag = false;

Arduino 代码:
// H 桥控制直流电机
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //引用I2C库
//I2C 引脚接A4, A5
//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x27,16,2);  
int input1 = 2;          // 定义uno的pin 2 向 input1 输出
int input2 = 3;          // 定义uno的pin 3 向 input2 输出
int input3 = 4;          // 定义uno的pin 4 向 input3 输出
int input4 = 5;          // 定义uno的pin 5 向 input4 输出
char data;
int dp1=0;
int dp2=0;
int fx=0;
int sj=0;
int mark = 0;

void setup() {
Serial.begin (9600);
//初始化各IO,模式为OUTPUT 输出模式
pinMode(input1,OUTPUT);
pinMode(input2,OUTPUT);
pinMode(input3,OUTPUT);
pinMode(input4,OUTPUT);
lcd.init();                  // 初始化LCD
lcd.backlight();             //设置LCD背景等亮
}

void loop() {
  recv_data();
  //show_data();
  updateServo();


}


void recv_data(){   //
  while(Serial.available()){
    data=Serial.read();
    if(data=='%'){
    dp1=Serial.read();
    delay(10);
    dp2=Serial.read();
    delay(10);
    fx=Serial.read();
    delay(10);
    sj=Serial.read();
    delay(10);
    }
     mark = 1;
  }
}

void updateServo(){
if(mark == 1){
if(fx==1){
for(int i=0;i<=30;i++){
  digitalWrite(input1,dp1);
  digitalWrite(input2,LOW);  
  digitalWrite(input3,dp2);
  digitalWrite(input4,LOW);  
  delay(5);  
}
}
if(fx==3){
//stop 停止
digitalWrite(input1,LOW);
digitalWrite(input2,LOW);  
digitalWrite(input3,LOW);
digitalWrite(input4,LOW);  
delay(5);  //延时0.5秒
}
if(fx==2){
//back 向后转
  for(int i=0;i<=30;i++){
  digitalWrite(input1,LOW);
  digitalWrite(input2,dp1);  
  digitalWrite(input3,LOW);
  digitalWrite(input4,dp2);  
  delay(5);
  }
  }
mark = 0;
}
}





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 23:22 , Processed in 0.045171 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表