如果变量不是 Serial.print-ed (Arduino),则不会更改值

问题描述 投票:0回答:1

Arduino 循环变量不会在图形边缘添加的嵌套循环内递增

我正在开发一个项目,该项目涉及使用 Arduino 根据多路复用器中的连接向图形添加边。该程序应该迭代嵌套循环来生成边的源顶点和目标顶点。然而,循环变量(

i
j
k
)似乎没有增加,并且预期输出(例如,值“2”)没有打印到串行监视器。以下是代码的相关部分:

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 16; j++)
    {
        for (int k = 0; k < 8; k++)
        {
            delay(10);
            int srcVertex = getGraphVertexID(&all_muxes[i], 'x', j);
            int destVertex = getGraphVertexID(&all_muxes[i], 'y', k);
            g.addEdge(srcVertex, destVertex);
        }
    }
}
Serial.println("2");

当取消注释

Serial.print
语句进行调试时,似乎
i
j
k
变量确实从其初始值发生了变化,这表明循环按预期运行。我得到了所需的输出(“2”);有趣的是,如果没有
delay(10);
,它也无法工作。

for (int i = 0; i < 2; i++)
{
    Serial.print("i = ");
    Serial.println(i);
    for (int j = 0; j < 16; j++)
    {
        Serial.print("j = ");
        Serial.println(j);
        for (int k = 0; k < 8; k++)
        {
            Serial.print("k = ");
            Serial.println(k);
            delay(10);
            int srcVertex = getGraphVertexID(&all_muxes[i], 'x', j);
            int destVertex = getGraphVertexID(&all_muxes[i], 'y', k);
            g.addEdge(srcVertex, destVertex);
        }
    }
}
Serial.println("2");

以下是我在

main.ino
文件中包含和初始化代码的方法:

#include "Arduino.h"
#include "pathfindingtest.h"

void setup(){
    Serial.begin(9600);
    Serial.println("before pathfinding");
    init_pathfinding();
}

我还对StandardCplusplus库中的

new_handler.cpp
进行了修改,如下:

#include <new>
#include "basic_definitions" // added this here

std::new_handler __new_handler;

std::new_handler std::set_new_handler(std::new_handler new_p) throw(){
    std::new_handler retval = __new_handler;
    __new_handler = new_p;
    return retval;
}

添加

Serial.print
行进行调试后,程序按预期运行。我无法确定具体原因。

这是我正在使用的库的 GitHub 存储库的链接。

我的大部分代码:

#pragma once

#include <StandardCplusplus.h>

#include <vector>
#include <list>
#include <queue>
#include <algorithm>
// #include <fstream>
#include <string>
#include <cstdio>

using namespace std;

// BREADBOARD OR MUX
enum DeviceType
{
    DEVICE,
    MULTIPLEXER,
    BREADBOARD
};

class Device
{
public:
    int num;
    DeviceType type;

    Device(int n, DeviceType t) : num(n), type(t) {}
};

class ConnectionNode
{
public:
    Device *device;
    int index;
    char connectionType;

    ConnectionNode(Device *d, int i, char type) : device(d), index(i), connectionType(type) {}
};

class Multiplexer : public Device
{
public:
    ConnectionNode *x[16];
    ConnectionNode *y[8];

    Multiplexer(int n) : Device(n, MULTIPLEXER)
    {
        for (auto &xi : x)
            xi = nullptr;
        for (auto &yi : y)
            yi = nullptr;
    }
};

class Breadboard : public Device
{
public:
    ConnectionNode *pin[24];

    Breadboard(int n) : Device(n, BREADBOARD)
    {
        for (auto &pin : pin)
            pin = nullptr;
    }
};

class Graph
{
public:
    int numVertices;
    list<int> *adjLists;
    bool *visited;
    bool *globalUsedPins;

    Graph(int vertices) : numVertices(vertices), adjLists(new list<int>[vertices]), visited(new bool[vertices]()), globalUsedPins(new bool[vertices]())
    {
        fill(visited, visited + vertices, false);
        fill(globalUsedPins, globalUsedPins + vertices, false);
    }

    ~Graph()
    {
        delete[] adjLists;
        delete[] visited;
        delete[] globalUsedPins;
    }

    void addEdge(int src, int dest)
    {
        adjLists[src].push_back(dest);
        adjLists[dest].push_back(src);
    }

    bool isSpecialPin(int pin)
    {
        int mainBreadboardStart = 2 * 24, mainBreadboardEnd = 2 * 24 + 23;
        int mcuBreadboardStart = 2 * 24 + 24, mcuBreadboardEnd = 2 * 24 + 31;
        return (pin >= mainBreadboardStart && pin <= mainBreadboardEnd) || (pin >= mcuBreadboardStart && pin <= mcuBreadboardEnd);
    }

    vector<int> findPathBFS(int startVertex, int endVertex)
    {
        //...
    }
};

int getGraphVertexID(const Device *device, char type, int pinIndex)
{
    //...
}

string printDeviceSpecifications(int vertexID)
{
    //...
}

void printMUXConections(const vector<int> &path)
{
    //...
}

struct PathRequest
{
    Device *startDevice;
    char startType;
    int startPin;
    Device *endDevice;
    char endType;
    int endPin;

    PathRequest(Device *startDevice, char startType, int startPin, Device *endDevice, char endType, int endPin)
        : startDevice(startDevice), startType(startType), startPin(startPin), endDevice(endDevice), endType(endType), endPin(endPin) {}
};

int findAndPrintPath(Graph &graph, const PathRequest &request)
{
   //...
}


int init_pathfinding() // initPathfinding
{   
    Serial.println("1");

    Multiplexer mux1(0), mux2(1);
    Breadboard main_breadboard(3), mcu_breadboard(4);

    Multiplexer all_muxes[2] = {mux1, mux2};

    int numVertices = 2 * 24 + 1 * 24 + 1 * 8;
    Serial.println("before constructor");

    Graph g(numVertices);
    Serial.println("after constructor");

    // Add edges to the graph every X to Y connection in the muxes
    for (int i = 0; i < 2; i++)
    {
        Serial.print("i = ");
        Serial.println(i);
        for (int j = 0; j < 16; j++)
        {
            Serial.print("j = ");
            Serial.println(j);
            for (int k = 0; k < 8; k++)
            {
                Serial.print("k = ");
                Serial.println(k);

                delay(10);

                int srcVertex = getGraphVertexID(&all_muxes[i], 'x', j);
                int destVertex = getGraphVertexID(&all_muxes[i], 'y', k);
                g.addEdge(srcVertex, destVertex);
            }
        }
    }
    Serial.println("2");

    // MUX1 pins edge connections FIXED
    g.addEdge(getGraphVertexID(&mux1, 'x', 0), getGraphVertexID(&mux2, 'y', 0));
    g.addEdge(getGraphVertexID(&mux1, 'x', 1), getGraphVertexID(&mux2, 'y', 1));
    g.addEdge(getGraphVertexID(&mux1, 'x', 2), getGraphVertexID(&mux2, 'y', 2));
    g.addEdge(getGraphVertexID(&mux1, 'x', 3), getGraphVertexID(&mux2, 'y', 3));
    g.addEdge(getGraphVertexID(&mux1, 'x', 4), getGraphVertexID(&mux2, 'y', 4));
    g.addEdge(getGraphVertexID(&mux1, 'x', 5), getGraphVertexID(&mux2, 'y', 5));
    g.addEdge(getGraphVertexID(&mux1, 'x', 6), getGraphVertexID(&mux2, 'y', 6));
    g.addEdge(getGraphVertexID(&mux1, 'x', 7), getGraphVertexID(&mux2, 'y', 7));
    g.addEdge(getGraphVertexID(&mux1, 'x', 8), getGraphVertexID(&main_breadboard, 'p', 12));
    g.addEdge(getGraphVertexID(&mux1, 'x', 9), getGraphVertexID(&main_breadboard, 'p', 13));
    g.addEdge(getGraphVertexID(&mux1, 'x', 10), getGraphVertexID(&main_breadboard, 'p', 14));
    g.addEdge(getGraphVertexID(&mux1, 'x', 11), getGraphVertexID(&main_breadboard, 'p', 15));
    g.addEdge(getGraphVertexID(&mux1, 'x', 12), getGraphVertexID(&main_breadboard, 'p', 16));
    g.addEdge(getGraphVertexID(&mux1, 'x', 13), getGraphVertexID(&main_breadboard, 'p', 17));
    g.addEdge(getGraphVertexID(&mux1, 'x', 14), getGraphVertexID(&main_breadboard, 'p', 18));
    g.addEdge(getGraphVertexID(&mux1, 'x', 15), getGraphVertexID(&main_breadboard, 'p', 19));

    g.addEdge(getGraphVertexID(&mux1, 'y', 0), getGraphVertexID(&mcu_breadboard, 'p', 0));
    g.addEdge(getGraphVertexID(&mux1, 'y', 1), getGraphVertexID(&mcu_breadboard, 'p', 1));
    g.addEdge(getGraphVertexID(&mux1, 'y', 2), getGraphVertexID(&mcu_breadboard, 'p', 2));
    g.addEdge(getGraphVertexID(&mux1, 'y', 3), getGraphVertexID(&mcu_breadboard, 'p', 3));
    g.addEdge(getGraphVertexID(&mux1, 'y', 4), getGraphVertexID(&mcu_breadboard, 'p', 4));
    g.addEdge(getGraphVertexID(&mux1, 'y', 5), getGraphVertexID(&mcu_breadboard, 'p', 5));
    g.addEdge(getGraphVertexID(&mux1, 'y', 6), getGraphVertexID(&mcu_breadboard, 'p', 6));
    g.addEdge(getGraphVertexID(&mux1, 'y', 7), getGraphVertexID(&mcu_breadboard, 'p', 7));

    // MUX2 pins edge connections FIXED
    g.addEdge(getGraphVertexID(&mux2, 'x', 0), getGraphVertexID(&main_breadboard, 'p', 20));
    g.addEdge(getGraphVertexID(&mux2, 'x', 1), getGraphVertexID(&main_breadboard, 'p', 21));
    g.addEdge(getGraphVertexID(&mux2, 'x', 2), getGraphVertexID(&main_breadboard, 'p', 22));
    g.addEdge(getGraphVertexID(&mux2, 'x', 3), getGraphVertexID(&main_breadboard, 'p', 23));
    g.addEdge(getGraphVertexID(&mux2, 'x', 4), getGraphVertexID(&main_breadboard, 'p', 0));
    g.addEdge(getGraphVertexID(&mux2, 'x', 5), getGraphVertexID(&main_breadboard, 'p', 1));
    g.addEdge(getGraphVertexID(&mux2, 'x', 6), getGraphVertexID(&main_breadboard, 'p', 2));
    g.addEdge(getGraphVertexID(&mux2, 'x', 7), getGraphVertexID(&main_breadboard, 'p', 3));
    g.addEdge(getGraphVertexID(&mux2, 'x', 8), getGraphVertexID(&main_breadboard, 'p', 4));
    g.addEdge(getGraphVertexID(&mux2, 'x', 9), getGraphVertexID(&main_breadboard, 'p', 5));
    g.addEdge(getGraphVertexID(&mux2, 'x', 10), getGraphVertexID(&main_breadboard, 'p', 6));
    g.addEdge(getGraphVertexID(&mux2, 'x', 11), getGraphVertexID(&main_breadboard, 'p', 7));
    g.addEdge(getGraphVertexID(&mux2, 'x', 12), getGraphVertexID(&main_breadboard, 'p', 8));
    g.addEdge(getGraphVertexID(&mux2, 'x', 13), getGraphVertexID(&main_breadboard, 'p', 9));
    g.addEdge(getGraphVertexID(&mux2, 'x', 14), getGraphVertexID(&main_breadboard, 'p', 10));
    g.addEdge(getGraphVertexID(&mux2, 'x', 15), getGraphVertexID(&main_breadboard, 'p', 11));


    vector<PathRequest> requests;
    
    int mcu_pin, main_pin;
    // std::cout << "Enter MCU pin: ";
    // std::cin >> mcu_pin;
    // std::cout << "Enter MAIN breadboard pin: ";
    // std::cin >> main_pin;

    mcu_pin = 1;
    main_pin = 1;
    Serial.println("10");


    PathRequest request(&main_breadboard, 'p', main_pin, &mcu_breadboard, 'p', mcu_pin);
    int path_found = findAndPrintPath(g, request);

    // std::cout << "Number of paths found: " << path_found << std::endl;

    Serial.println("pathfinding init");

    return 0;
}

c++ loops arduino path-finding arduino-c++
1个回答
0
投票

我使用

valgrind
工具来测量我的程序的使用情况。看起来它使用大约 28kb 内存,而 arduino 有 2kb:

valgrind --tool=massif ./pathfinding.out
==28763== Massif, a heap profiler
==28763== Copyright (C) 2003-2017, and GNU GPL'd, by Nicholas Nethercote
==28763== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
==28763== Command: ./pathfinding.out
==28763== 
Enter MCU pin: 6
Enter MAIN breadboard pin: 2

Path from  -> MainBreadboard 2  to  -> MCUBreadboard 6  is: 
 -> MainBreadboard 2  -> MUX2 x6  -> MUX2 y0  -> MUX1 x0  -> MUX1 y6  -> MCUBreadboard 6 
All MUX conections: 
SetConection(1001, x6 , y0 )
SetConection(1000, x0 , y6 )


Number of paths found: 1
==28763== 

所以我遇到的错误肯定是因为这个

© www.soinside.com 2019 - 2024. All rights reserved.