|
#include <stdlib.h>
#include <Wire.h>
#include "Adafruit_MCP23017.h"
#include <Servo.h> // 声明调用舵机Servo.h库
#include <stdio.h>
#include <assert.h>
#define er 111
#define san 1
#define si 2
#define wu 3
#define liu 4
#define qi 5
#define ba 6
#define jiu 7
#define fuwei 88
#define qieguan 77
#define songliao 55
#define duoji 66
#define PUL 87//脉冲
#define latchPin 100
#define clockPin 101
#define dataPin 111 //这里定义移位寄存器三个脚
char DIR = 1; //步进方向
int steps = 480 ; //步进次数
int weimiao = 5; //步进速度
unsigned char data = 0;//定义了一个字节用于IO扩展
int bushu=66;//步进电机拉料步数
int yuliaobushu = 3;//步进电机余料步数
int yuliaobushu2 = 10;//步进电机余料步数
char duanliao = 1; //短料判断
char qiting = 0; //启动/停止
long numdata[4]={0,0,0,0}; //record value
String recData = ""; //recording received data from upper machine
int mark = 0;
char dat[64]="";
int curSetData = 0;
double chuankou;
float chuankou1 =22.22;
String ceshi = "456.1A3";
char* b="A12.34";
char* a;
Adafruit_MCP23017 mcp;
Servo myservo; // 创建一个舵机对象
//int pos = 0; // 变量pos用来存储舵机位置
int zhidingjiaodu = 100; // 变量jiaodu用来指定舵机转动位置
int duojifuwei = 0; // 舵机复位角度
int duojisudu = 10; // 舵机速度
void setup ()
{
Serial.begin(9600);
//Wire.begin(4,5);
myservo.attach(9); // 将引脚9上的舵机与声明的舵机对象连接起来
// pinMode(2, OUTPUT); //前推气夹
// pinMode(3, OUTPUT); //主电机
// pinMode(4, INPUT ); //步进电机复位接近开关
// pinMode(5, OUTPUT); //拔料气缸
// pinMode(6, OUTPUT); //步进电机脉冲引脚
// pinMode(7, OUTPUT); //步进电机方向
// pinMode(8, INPUT); //检测料接近开关
// pinMode(9,OUTPUT); //舵机引脚
// /*
mcp.begin(); //使用默认地址0(第一个从站)
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT); //让三个脚都是输出状态
pinMode(fuwei,INPUT);
pinMode(53,OUTPUT);
mcp.pinMode(0, OUTPUT); //前推气夹
mcp.pinMode(san, OUTPUT); //主电机
mcp.pinMode(si, INPUT ); //步进电机复位接近开关
mcp.pullUp(si, HIGH);//在内部打开100K上拉
mcp.pinMode(wu, OUTPUT); //拔料气缸
mcp.pinMode(liu, OUTPUT); //步进电机脉冲引脚
mcp.pinMode(qi, OUTPUT); //步进电机方向
mcp.pinMode(ba, INPUT); //检测料接近开关
mcp.pullUp(ba, HIGH);//在内部打开100K上拉
mcp.pinMode(jiu,OUTPUT); //舵机引脚
//*/
mcp.digitalWrite(san, HIGH);//停止电机
mcp.digitalWrite(er, HIGH);
mcp.digitalWrite(wu, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
//serial_get();
//if(qiting){
//zhuchengxu();
//}
delay(1000);
mcp.digitalWrite(0, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
mcp.digitalWrite(0, LOW);
}
void zhuchengxu(){
duanliao = 0;
if(!mcp.digitalRead(ba)) duanliao = 1; //短料判断
mcp.digitalWrite(er, LOW);//前推夹紧
mcp.digitalWrite(wu, HIGH);//压料气缸释放
mcp.digitalWrite(san, LOW);//启动电机
while(mcp.digitalRead(si)){
duoji1();
bujin(1);
}
mcp.digitalWrite(san, HIGH);//停止电机
mcp.digitalWrite(wu, LOW);//压料气缸夹紧
mcp.digitalWrite(er, HIGH);//前推释放
for(int i=0;i<bushu;i++){ //
if(!mcp.digitalRead(ba) && duanliao == 0) {
mcp.digitalWrite(er, LOW);//前推夹紧
mcp.digitalWrite(wu, HIGH);//压料气缸释放
mcp.digitalWrite(san, LOW);//启动电机
while(mcp.digitalRead(si)){
duoji1();
bujin(1);
}
mcp.digitalWrite(san, HIGH);//停止电机
mcp.digitalWrite(wu, LOW);//压料气缸夹紧
mcp.digitalWrite(er, HIGH);//前推释放
for(int i=0;i<yuliaobushu;i++){
bujin(0);
}
duanliao = 0; //短料复位
break;
}
mcp.digitalWrite(qi, LOW); //控制步进电机方向
//io(6,DIR); //控制步进电机方向
for(int i=0;i<480;i++){ //steps代表走多少次细分(行程)
mcp.digitalWrite(liu, HIGH); //逆时针(正转),无细分,半流,200脉冲/转,步距角1.8度,500KHZ
//delay(1); //延时1毫秒
delayMicroseconds(weimiao);
mcp.digitalWrite(liu, LOW);
//delay(1); //延时一毫秒
//delayMicroseconds(weimiao);
}
}
mcp.digitalWrite(er, LOW);//前推夹紧
mcp.digitalWrite(wu, HIGH);//压料气缸释放
}
//将字符串转换为数字
void zhuanhuan(){
for(int i = 1; i<recData.length(); i++){
recData[i-1] = recData;
}
}
//串口接收并解析命令函数
void serial_get(){
while (Serial.available() > 0)
{
recData += char(Serial.read());
delay(2);
mark = 1;
}
if (mark == 1)
{
if (recData[0] == 'A')
{
Serial.println(chuankou1);
chuankou1 =0;
zhuanhuan();
steps = 20*recData.toFloat(); //此处将mm单位转化为脉冲
//Serial.println(recData);
Serial.println(steps);
}
else if (recData[0] == 'B')
{
zhuanhuan();
zhidingjiaodu = recData.toFloat(); //给舵机赋角度值
// Serial.println(recData);
Serial.println(zhidingjiaodu);
}
else if (recData[0] == 'C')
{
zhuanhuan();
duojisudu = recData.toFloat(); //给舵机赋速度值
//Serial.println(recData);
Serial.println(duojisudu);
}
else if (recData[0] == 'D')
{
qiting = 1;
Serial.println(qiting);
}
else if (recData[0] == 'E')
{
qiting = 0;
Serial.println(qiting);
}
recData = String("");
mark = 0;
}
}
/*
if(comdata[0] == 'A')
{
Serial.println("判断成功");
Abujin[0]=atoi(comdata[1]);
Abujin[0]=(comdata[1] - '0');//在此把comdata转化成INT型数值,以备后续使用
Serial.println(Abujin[0]);
comdata = "";// 必须在此把comdata设为空字符,否则会导致前后字符串叠加
*/
void duoji1(){
for(int pos = 0; pos < zhidingjiaodu; pos++){ // 舵机从0°转到180°,每次增加1°
myservo.write(pos); // 给舵机写入角度
delay(duojisudu); // 延时速度
//if(qieguan==LOW) return;
}
myservo.write(0);//舵机的复位角度
}
//void duoji1(){
// for(pos = 0; pos < 270; pos += 1){ // 舵机从0°转到180°,每次增加1°
// myservo.write(pos); // 给舵机写入角度
//delay(8); // 延时15ms让舵机转到指定位置
//if(qieguan==LOW) return;
//}
//}
void bujin(int DIR){
mcp.digitalWrite(qi, DIR); //控制步进电机方向
//io(6,DIR); //控制步进电机方向
for(int i=0;i<steps;++i){ //steps代表走多少次细分(行程)
mcp.digitalWrite(0, HIGH); //逆时针(正转),无细分,半流,200脉冲/转,步距角1.8度,500KHZ
delay(100); //延时1毫秒
//delayMicroseconds(20);
mcp.digitalWrite(0, LOW);
//delay(1); //延时一毫秒
//delayMicroseconds(weimiao);
}
}
//void bujin(int DIR){
// io(6,DIR);//控制步进电机方向
// for(int i=0;i<steps;++i){
// digitalWrite(PUL,HIGH);//逆时针(正转),无细分,半流,200脉冲/转,步距角1.8度,500KHZ
// delayMicroseconds(weimiao);
// digitalWrite(PUL,LOW);
// delayMicroseconds(weimiao);
// }
//}
unsigned char bit_set(unsigned char *p_data, unsigned char position, int flag)
{
if (flag == 1)
{
*p_data |= (1 << (position - 1));//0000 0010或0000 0001
} // 0000 0011
else if (flag == 0)
{
*p_data &= ~(1 << (position - 1));//0000 0010&1111 1110
} // 0000 0010
return *p_data;
}
void io(unsigned char position, int flag){
digitalWrite(latchPin,LOW); //将ST_CP口上面加低电平让芯片准备好接收数据
shiftOut(dataPin,clockPin,MSBFIRST,bit_set(&data,position,flag));
digitalWrite(latchPin,HIGH); //将ST_CP这个针脚恢复到高电平
}
// Serial.println(bit_set(&data,1,1));
// io(2,1);
/*
unsigned char bit_set(unsigned char *p_data, unsigned char position, int flag)
{
int a = 1<<(position-1);
if (flag)
{
*p_data |= a;
}
else
{
*p_data &= ~a;
}
return *p_data;
}
*/
/*
while(mcp.digitalRead(2)){ //步进电机复位
bujin(LOW);
if(!mcp.digitalRead(6)){
mcp.digitalWrite(0, HIGH);//前推夹紧
mcp.digitalWrite(1, HIGH);//启动电机
for(int i=0;i<yuliaobushu;i++){
duoji1();
bujin(1);
}
mcp.digitalWrite(1, LOW);//停止电机
mcp.digitalWrite(3, HIGH);//压料气缸夹紧
mcp.digitalWrite(0, LOW);//前推释放
for(int i=0;i<yuliaobushu2;i++){
bujin(LOW);
}
}
}
*/
/*
void zhuchengxu(){
mcp.digitalWrite(0, HIGH);//前推夹紧
mcp.digitalWrite(1, HIGH);//启动电机
for(int i=0;i<bushu;i++){
duoji1();
bujin(1);
}
mcp.digitalWrite(1, LOW);//停止电机
mcp.digitalWrite(3, HIGH);//压料气缸夹紧
mcp.digitalWrite(0, LOW);//前推释放
while(mcp.digitalRead(2)){ //步进电机复位
bujin(LOW);
if(!mcp.digitalRead(6)){
mcp.digitalWrite(0, HIGH);//前推夹紧
mcp.digitalWrite(1, HIGH);//启动电机
for(int i=0;i<yuliaobushu;i++){
duoji1();
bujin(1);
}
mcp.digitalWrite(1, LOW);//停止电机
mcp.digitalWrite(3, HIGH);//压料气缸夹紧
mcp.digitalWrite(0, LOW);//前推释放
for(int i=0;i<yuliaobushu2;i++){
bujin(LOW);
}
}
}
// io(2,1);//前推夹紧
// io(3,1);//启动电机
// io(3,0);//停止电机
// io(5,1);//压料气缸夹紧
// io(2,0);//前推释放
//while(fuwei!=LOW){
// bujin(0);
// }
// io(2,1);//前推夹紧
// io(5,0);//手指气缸释放
}
//串口接收并解析命令函数
void serial_get(){
while (Serial.available() > 0)
{
recData += char(Serial.read());
delay(2);
mark = 1;
}
// Serial.println(ceshi);
// Serial.println(chuankou1);
// chuankou1 = ceshi.toFloat();
// Serial.println(ceshi);
// Serial.println(chuankou1);
if (mark == 1)
{
if (recData[0] == 'A')
{
// Serial.println(ceshi);
// chuankou1 = ceshi.toFloat();
// Serial.println(a);
// Serial.println("MCU:"+recData);
// chuankou = atof(recData);
// chuankou1 = atof(b);
// changdu = 0; //从新写入前归零
Serial.println(chuankou1);
chuankou1 =0;
for(int i = 1; i<recData.length(); i++){
// AA = Abujin[5]*1000 + Abujin[4]*1000 + Abujin[3]*1000 + Abujin[2]*100 + Abujin[1]*10 + Abujin[0]*1;
//changdu += recData;
// changdu += (comdata[1] - '0');//在此把comdata转化成INT型数值,以备后续使用
// Serial.println(chuankou1);
// Serial.println(recData);
// chuankou = atof("123.12");
// Serial.println(chuankou);
recData[i-1] = recData;
}
steps = 20*recData.toFloat(); //此处将mm单位转化为脉冲
Serial.println(recData);
Serial.println(steps);
}
else if (recData[0] == 'B')
{
}
else if (recData[0] == 'C')
{
}
else if (recData[0] == 'D')
{
}
recData = String("");
mark = 0;
}
}
*/
|
|