接入arduino后42电机无法正常工作
-
rt,孩子快崩溃了,为什么接入arduino后在arduino串口操作中能够正常输出数据到串口监视器,但是无法执行电机命令,换过其他硬串口软串口都不行,求求大佬救一救
import sensor, image, time import json from machine import UART #from pyb import UART # For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... yellow_threshold = (57, 78, -32, 6, -30, 36) # You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings. x = 0 y = 0 judge = '0' sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.set_framerate(40) sensor.skip_frames(time = 2000 ) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS. left_roi = [0,0,80,200] # OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7, OpenMV2 M4 的UART(3)是P4-TX P5-RX uart = UART(3, 115200) #OpenMV RT 注释掉这一行,用下一行UART(1) #uart = UART(1, 115200) #OpenMV RT 用UART(1)这行,注释掉上一行UART(3) # OpenMV RT 只有串口UART(1),对应P4-TX P5-RX; OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7 的UART(1)是P0-RX P1-TX x = 0 y = 0 judge = '0' while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. blobs = img.find_blobs([yellow_threshold],roi=left_roi,merge=True,area_threshold=200) if blobs: data=[] for b in blobs: # Draw a rect around the blob. img.draw_rectangle(b.rect()) # rect img.draw_cross(b.cx(), b.cy()) # cx, cy if b.cy() < 25: x = x + 1 if x > 4 : if judge == '0': x = 0 data.append((b.cx(),b.cy())) data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('L') judge = '1' elif b.cy() > 27: y = y + 1 if y > 4: if judge == '0': data.append((b.cx(),b.cy())) data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('R') judge = '1' else: judge = '0' print('---------') else: print("not found!") #{(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
下面的是arduino的代码:
#include <ArduinoJson.h> #include <SoftwareSerial.h> SoftwareSerial mySerial(9, 11); // RX, TX pins for SoftwareSerial typedef struct { int data[50][2] = {{0, 0}}; int len = 0; } List; List list; int leftindex = 0; int rightindex = 0; const int xdirPin = 5; // X方向控制引脚 const int xstepPin = 2; // X步进控制引脚 const int ydirPin = 6; // Y方向控制引脚 const int ystepPin = 3; // Y步进控制引脚 const int zdirPin = 7; // Z方向控制引脚 const int zstepPin = 4; // Z步进控制引脚 const int astepPin = 12; // A步进控制引脚 const int adirPin = 13; // A方向控制引脚 const int enablePin = 8; // 使能控制引脚 void setup() { pinMode(xstepPin, OUTPUT); // X步进引脚为输出模式 pinMode(xdirPin, OUTPUT); // X方向引脚为输出模式 pinMode(astepPin, OUTPUT); // A步进引脚为输出模式 pinMode(adirPin, OUTPUT); // A方向引脚为输出模式 pinMode(ystepPin, OUTPUT); // Y步进引脚为输出模式 pinMode(ydirPin, OUTPUT); // Y方向引脚为输出模式 pinMode(zstepPin, OUTPUT); // Z步进引脚为输出模式 pinMode(zdirPin, OUTPUT); // Z方向引脚为输出模式 pinMode(enablePin, OUTPUT); // 使能控制引脚为输出模式 digitalWrite(enablePin, LOW); // 低电平使能电机驱动 Serial.begin(115200); // 初始化硬件串口通信 mySerial.begin(115200); // 初始化软串口通信 } void ahead() { digitalWrite(xdirPin, HIGH); digitalWrite(ydirPin, LOW); digitalWrite(adirPin, LOW); digitalWrite(zdirPin, HIGH); } void back() { digitalWrite(xdirPin, LOW); digitalWrite(ydirPin, HIGH); digitalWrite(adirPin, HIGH); digitalWrite(zdirPin, LOW); } void left() { digitalWrite(xdirPin, LOW); digitalWrite(ydirPin, LOW); digitalWrite(zdirPin, HIGH); digitalWrite(adirPin, HIGH); } void right() { digitalWrite(xdirPin, HIGH); digitalWrite(ydirPin, HIGH); digitalWrite(zdirPin, LOW); digitalWrite(adirPin, LOW); } void turnround() { digitalWrite(xdirPin, HIGH); digitalWrite(ydirPin, HIGH); digitalWrite(zdirPin, HIGH); digitalWrite(adirPin, HIGH); } void run(int speed) { digitalWrite(xstepPin, HIGH); digitalWrite(ystepPin, HIGH); digitalWrite(zstepPin, HIGH); digitalWrite(astepPin, HIGH); delayMicroseconds(1200 - speed); digitalWrite(xstepPin, LOW); digitalWrite(ystepPin, LOW); digitalWrite(zstepPin, LOW); digitalWrite(astepPin, LOW); delayMicroseconds(1200 - speed); } String detectString() { while(mySerial.read() != '{'); return(mySerial.readStringUntil('}')); } void clearList() { memset(list.data, 0, sizeof(list.data)); // 清空数据列表 list.len = 0; } void getList() { String s = detectString(); // 从串口读取数据 String numStr = ""; for (int i = 0; i < s.length(); i++) { if (s[i] == '(') { numStr = ""; } else if (s[i] == ',') { list.data[list.len][0] = numStr.toInt(); numStr = ""; } else if (s[i] == ')') { list.data[list.len][1] = numStr.toInt(); numStr = ""; list.len++; } else { numStr += s[i]; } } } void loop() { // 运行步进电机 if (mySerial.available()) { getList(); // 读取并解析串口数据 for (int i = 0; i < list.len; i++) { if (list.data[i][1] < 25) { Serial.println("left"); left(); }else if (list.data[i][1] > 28) { Serial.println("right"); right(); }else{ Serial.println("ahead"); } clearList(); // 清空数据列表 } run(800); } ahead(); run(400); }