完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我有一个项目来模拟指南针的航向和两个直流电机的运动。当我在 arduino nano/uno 上操作它时,没问题。但是当我在 nodemcu 上操作它时,罗盘读数不知何故不会出现在串行监视器上。有人知道 nodemcu 上的 SDA 或 SCL 吗?
这是我使用的代码 代码:全选#include <tinyGPS++.h> #include #include #include //#include #define RX_PIN 2 #define TX_PIN 3 #define LED_PIN 13 #define GPS_BAUD 9600 #define WAYPOINT_THRESHHOLD 3 int LEFT_MOTOR[3] = {8, 4, 5}; int RIGHT_MOTOR[3] = {9, 6, 7}; TinyGPSPlus gps; //const char* ssid = "wifi kami"; //ssid of your wifi //const char* password = "cipicung"; //password of your wifi //float latitude , longitude; //int hour , minute , second; //String time_str , lat_str , lng_str; //int pm; //WiFiServer server(80); int wp = 0; bool selesai = false; QMC5883LCompass compass; SoftwareSerial ss(RX_PIN, TX_PIN); int calcLeftMotor(double relBearing) { if (relBearing > 0) return 255; else return 1.425 * relBearing + 255; } int calcRightMotor(double relBearing) { if (relBearing <= 0) return 255; else return -1.425 * relBearing + 255; } //KALAU NGASIH RAW DATA MAGNETIK (if give raw magnetic data) double calcHeading (int x, int y) { return -atan2(x, y) * 180 / 3.141592654; } void setup() { Serial.begin(9600); ss.begin(GPS_BAUD); pinMode(LED_PIN, OUTPUT); //LED pinMode (LEFT_MOTOR, OUTPUT); pinMode (RIGHT_MOTOR, OUTPUT); // WiFi.begin("wifi kami", "cipicung"); //connecting to wifi // while (WiFi.status() != WL_CONNECTED)// while wifi not connected // Serial.println(""); // Serial.println("WiFi connected"); // server.begin(); // Serial.println("Server started"); // Serial.println(WiFi.localIP()); Serial.println("Menuju Waypoint"); //init compass compass.init(); } void loop() { double targetLat[] = {-6.992833}; double targetLon[] = {107.642863}; int jmlWP = sizeof(targetLat)/4; while (ss.available() > 0) gps.encode(ss.read()); if(wp >= jmlWP) selesai = true; double dist = 0; if (gps.location.isValid()) { dist = distance(targetLat[wp], targetLon[wp], gps.location.lat(), gps.location.lng()); Serial.print("distance to target: "); Serial.println(dist); double bearing = 0; //target angle compass.read(); // reads from compass double heading = calcHeading(compass.getX(), compass.getY()); double relBearing = bearing - heading; int leftSpeed = map(calcLeftMotor(relBearing),0,255,130,150); int rightSpeed = map(calcRightMotor(relBearing),0,255,130,150); if(selesai){ digitalWrite(LEFT_MOTOR[1], 0); digitalWrite(LEFT_MOTOR[2], 0); analogWrite(LEFT_MOTOR[0], 0); // RIGHT_MOTOR digitalWrite(RIGHT_MOTOR[1], 0); digitalWrite(RIGHT_MOTOR[2], 0); analogWrite(RIGHT_MOTOR[0], 0); }else if (dist > WAYPOINT_THRESHHOLD) { //LEFT_MOTOR digitalWrite(LEFT_MOTOR[1], 1); digitalWrite(LEFT_MOTOR[2], 0); analogWrite(LEFT_MOTOR[0], leftSpeed); // RIGHT_MOTOR digitalWrite(RIGHT_MOTOR[1], 0); digitalWrite(RIGHT_MOTOR[2], 1); analogWrite(RIGHT_MOTOR[0], rightSpeed); }else if(dist <= WAYPOINT_THRESHHOLD){ wp++; } // if (dist < WAYPOINT_THRESHHOLD) // digitalWrite(LED_PIN, HIGH); // // if (dist > WAYPOINT_THRESHHOLD) // digitalWrite(LED_PIN, LOW); Serial.print(" Heading: "); Serial.println(heading); Serial.print("Left: "); Serial.print(leftSpeed); Serial.print(" Right: "); Serial.println(rightSpeed); Serial.println(" "); delay (1000); } } double distance(double lat1, double lon1, double lat2, double lon2) { // Conversion factor from degrees to radians (pi/180) const double toRadian = 0.01745329251; // First coordinate (Radians) double lat1_r = lat1 * toRadian; double lon1_r = lon1 * toRadian; // Second coordinate (Radians) double lat2_r = lat2 * toRadian; double lon2_r = lon2 * toRadian; // Delta coordinates double deltaLat_r = (lat2 - lat1) * toRadian; double deltaLon_r = (lon2 - lon1) * toRadian; // Distance double a = sin(deltaLat_r / 2) * sin(deltaLat_r / 2) + cos(lat1_r) * cos(lat2_r) * sin(deltaLon_r / 2) * sin(deltaLon_r / 2); double c = 2 * atan2(sqrt(a), sqrt(1 - a)); double distance = 6371 * c * 1000; return distance; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
544浏览 6评论
457浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
435浏览 5评论
441浏览 4评论
410浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-20 14:33 , Processed in 0.765808 second(s), Total 74, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号