简体   繁体   English

未在此范围内声明

[英]not declared in this scope

An error occurred during the uploading the code to Arduino Atmega2560. 将代码上传到Arduino Atmega2560时发生错误。

The errors are: 错误是:

herkulex.cpp: In member function 'void herkulex::hklx_SendPacket(DrsPacket)':
herkulex.cpp:121: error: 'USART0_PutNChar' was not declared in this scope
herkulex.cpp: In member function 'unsigned char herkulex::hklx_ucReceivePacket(DrsPacket*)':
herkulex.cpp:211: error: 'USART0_ucGetNChar' was not declared in this scope
herkulex.cpp: In member function 'void herkulex::hklx_RemoveInvalidData()':
herkulex.cpp:244: error: 'USART0_ucTrashNChar' was not declared in this scope

Below is the code where the error occurred: (herkulex.cpp code) 以下是发生错误的代码:(herkulex.cpp代码)

#include<Arduino.h>
#include<Wire.h>
#include<Math.h> 

//ATmega128ì�˜ ë ˆì§€ìŠ¤í„° 등ì�´ ì •ì�˜ë�˜ì–´ 있ì�Œ
#include <avr/io.h>
//#include "TIMER0.h"
#include "USART0.h" 
//#include "USART0.c"

//#define __HERKULEX_C
#include "herkulex.h"
//#undef __HERKULEX_C

//void hklx_RemoveInvalidData(void);
#define herkulex_cpp

herkulex::herkulex()
{
  // Empty constructor. needed for lib
}



#define USART_BUF_SIZE 256  
#define PROTOCOL_SIZE_IDX 2
#define PROTOCOL_ID_IDX   3
#define PROTOCOL_CMD_IDX  4
#define PROTOCOL_CS1_IDX  5
#define PROTOCOL_CS2_IDX  6
#define PROTOCOL_DATA_IDX  7

//í—¤ë�” ê´€ë ¨
#define HEADER      0xFF

//SIZE ê´€ë ¨
#define MIN_PACKET_SIZE     7
#define MIN_ACK_PACKET_SIZE                 9
#define MAX_PACKET_SIZE                     223
#define MAX_DATA_SIZE                       (MAX_PACKET_SIZE-MIN_PACKET_SIZE)

//ID ê´€ë ¨
#define MAX_ID                              0xFD    
#define BROADCAST_ID                        0xFE  

//CMD ê´€ë ¨ - Request Packet
#define CMD_EEP_WRITE                       0x01
#define CMD_EEP_READ                        0x02
#define CMD_RAM_WRITE                       0x03
#define CMD_RAM_READ                        0x04    
    #define CMD_RW_DATA_ADDR_IDX            7
    #define CMD_RW_DATA_LEN_IDX             8
#define CMD_I_JOG                           0x05
    #define CMD_I_JOG_STRUCT_SIZE           5
    #define CMD_I_JOG_MAX_DRS               (MAX_DATA_SIZE/CMD_I_JOG_STRUCT_SIZE)
#define CMD_S_JOG                           0x06
    #define CMD_S_JOG_STRUCT_SIZE           4
    #define CMD_S_JOG_MAX_DRS               (MAX_DATA_SIZE/CMD_S_JOG_STRUCT_SIZE)
#define CMD_STAT                            0x07    
#define CMD_ROLLBACK                        0x08
#define CMD_REBOOT                          0x09

#define CMD_MIN                             (CMD_EEP_WRITE)
#define CMD_MAX                             (CMD_REBOOT)

//CMD ê´€ë ¨ - ACK Packet
#define CMD_ACK_MASK                        0x40

#define CMD_EEP_WRITE_ACK                   (CMD_EEP_WRITE|CMD_ACK_MASK)
#define CMD_EEP_READ_ACK                    (CMD_EEP_READ|CMD_ACK_MASK)
#define CMD_RAM_WRITE_ACK                   (CMD_RAM_WRITE|CMD_ACK_MASK)
#define CMD_RAM_READ_ACK                    (CMD_RAM_READ|CMD_ACK_MASK)
#define CMD_I_JOG_ACK                       (CMD_I_JOG|CMD_ACK_MASK)
#define CMD_S_JOG_ACK                       (CMD_S_JOG|CMD_ACK_MASK)
#define CMD_STAT_ACK                        (CMD_STAT|CMD_ACK_MASK)
#define CMD_ROLLBACK_ACK                    (CMD_ROLLBACK|CMD_ACK_MASK)
#define CMD_REBOOT_ACK                      (CMD_REBOOT|CMD_ACK_MASK)

#define CMD_ACK_MIN                         (CMD_EEP_WRITE_ACK)
#define CMD_ACK_MAX                         (CMD_REBOOT_ACK)

//CheckSum ê´€ë ¨
#define CHKSUM_MASK                         0xFE



//////////////////////////////receiving state result value //////////////////////////////
  enum{
    DRS_RXWAITING,
    DRS_RXCOMPLETE,
    DRS_HEADERNOTFOUND,
    DRS_INVALIDSIZE,
    DRS_UNKNOWNCMD,
    DRS_INVALIDID,
    DRS_CHKSUMERROR,
    DRS_RXTIMEOUT
  }DrsRxStatus;

void herkulex:: hklx_Init(unsigned long ulBaudRate){
//  TIMER0_Init();
//  USART0_Init(ulBaudRate);
    return;
}
#define HEADER      0xFF
void herkulex:: hklx_SendPacket(DrsPacket stPacket){
    unsigned char i;

    //í—¤ë�” ìž…ë ¥
    stPacket.ucHeader[0] = HEADER;
    stPacket.ucHeader[1] = HEADER;

    //CheckSum 계산 후 ìž…ë ¥
    stPacket.ucCheckSum1 = stPacket.ucPacketSize ^ stPacket.ucChipID ^ stPacket.ucCmd;
    for( i=0; i<(stPacket.ucPacketSize-MIN_PACKET_SIZE); i++ )    
        stPacket.ucCheckSum1 ^= stPacket.unData.ucData[i];

    stPacket.ucCheckSum2 = ~(stPacket.ucCheckSum1);
    stPacket.ucCheckSum1 &= CHKSUM_MASK;    
    stPacket.ucCheckSum2 &= CHKSUM_MASK;

    //PacketSize 만í�¼ USART0으로 ì†¡ì‹ 
    USART0_PutNChar(&stPacket.ucHeader[0], stPacket.ucPacketSize);

    return;
}

unsigned char herkulex:: hklx_ucReceivePacket(DrsPacket *pstPacket){
    unsigned char i, ucPacketSize, ucChipID, ucCmd, ucReadLen, ucCheckSum1, ucCheckSum2;

    //ë°›ì�€ ë�°ì�´í„° 수가 ACK 패킷ì�˜ 최소 사ì�´ì¦ˆë³´ë‹¤ ì �ì�„ 경우
    if(gucRxBufferCnt < MIN_ACK_PACKET_SIZE){
        return DRS_RXWAITING;
    }

    //패킷� 처� � 바�트가 헤�가 아� 경우
    if(gucRxBuffer[gucRxBufferReadIdx] != HEADER || 
       gucRxBuffer[((unsigned int)gucRxBufferReadIdx+1)%USART_BUF_SIZE] != HEADER){
        hklx_RemoveInvalidData();
        return DRS_HEADERNOTFOUND;   
    }
    //맞� 경우 Size, ID, Cmd를 �어옴
    else{
        ucPacketSize = gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + PROTOCOL_SIZE_IDX ) % USART_BUF_SIZE ];
        ucChipID = gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + PROTOCOL_ID_IDX ) % USART_BUF_SIZE ];
        ucCmd = gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + PROTOCOL_CMD_IDX ) % USART_BUF_SIZE ];
    }

    //Size가 최대보다 � 경우
    if(ucPacketSize > MAX_PACKET_SIZE){
        hklx_RemoveInvalidData();
        return DRS_INVALIDSIZE;
    }
    //아� Size 만�� ��터가 들어오지 않았� 경우
    else if(ucPacketSize > gucRxBufferCnt){
        return DRS_RXWAITING;
    }

    //Cmd가 범위를 벗어난 경우
    if(!(ucCmd >= CMD_ACK_MIN && ucCmd <= CMD_ACK_MAX)){
        hklx_RemoveInvalidData();
        return DRS_UNKNOWNCMD;
    }

    //ID가 범위를 벗어난 경우
    if(ucChipID >= BROADCAST_ID){
        hklx_RemoveInvalidData();
        return DRS_INVALIDID;
    }

    //ê°� Cmdì—� ë”°ë�¼ì„œ Sizeê°€ ì �합한지 검사
    switch(ucCmd){
        case CMD_EEP_WRITE_ACK:
        case CMD_RAM_WRITE_ACK:
        case CMD_I_JOG_ACK:
        case CMD_S_JOG_ACK:
        case CMD_STAT_ACK:
        case CMD_ROLLBACK_ACK:
        case CMD_REBOOT_ACK:
            if(ucPacketSize != MIN_ACK_PACKET_SIZE){
                hklx_RemoveInvalidData();
                return DRS_INVALIDSIZE;
            }
            break;
        case CMD_EEP_READ_ACK:
        case CMD_RAM_READ_ACK:
            ucReadLen = gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + CMD_RW_DATA_LEN_IDX ) % USART_BUF_SIZE ];
            if(ucPacketSize != (MIN_ACK_PACKET_SIZE + 2 + ucReadLen)){
                hklx_RemoveInvalidData();
                return DRS_INVALIDSIZE;
            }   
            break;
    }

    //ìˆ˜ì‹ ë�œ 패킷ì�˜ CheckSumì�„ 계산
    ucCheckSum1 = ucPacketSize ^ ucChipID ^ ucCmd;
    for(i=0;i<(ucPacketSize-MIN_PACKET_SIZE);i++){
        ucCheckSum1 ^= gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + i ) % USART_BUF_SIZE ];
    }
    ucCheckSum2 = ~(ucCheckSum1);
    ucCheckSum1 &= CHKSUM_MASK;
    ucCheckSum2 &= CHKSUM_MASK;

    //계산ë�œ CheckSumì�´ ì‹¤ì œë¡œ ë°›ì�€ ë�°ì�´í„°ì™€ ì�¼ì¹˜í•˜ëŠ”지 검사
    if(ucCheckSum1 != gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + PROTOCOL_CS1_IDX) % USART_BUF_SIZE ] ||
       ucCheckSum2 != gucRxBuffer[ ((unsigned int)gucRxBufferReadIdx + PROTOCOL_CS2_IDX) % USART_BUF_SIZE ])
    {
        hklx_RemoveInvalidData();
        return DRS_CHKSUMERROR;
    }       

    //ëª¨ë“  검사를 통과했다면, 받는 pstPacketì—� 패킷ì�„ 복사
    USART0_ucGetNChar(&(pstPacket->ucHeader[0]), ucPacketSize);

    //완료 �태를 반환
    return DRS_RXCOMPLETE;
}

void herkulex:: hklx_RemoveInvalidData(void){
    unsigned char ucRcvCnt, ucRdIndex, i;

    //버�� �태를 변수� 복사
    ucRcvCnt=gucRxBufferCnt;
    ucRdIndex=gucRxBufferReadIdx;

    //받� ��터가 없� 경우 반환
    if(ucRcvCnt==0){
        return;
    }

    //다ì�Œ í—¤ë�”를 찾거나 ë°›ì�€ ë�°ì�´í„°ì�˜ ë��ì—� ë�„ë‹¬í•  때까지 진행

    for(i=1;i<ucRcvCnt-1;i++){
        if( (gucRxBuffer[ ((unsigned int)ucRdIndex + i ) % USART_BUF_SIZE ] == HEADER ) &&
            (gucRxBuffer[ ((unsigned int)ucRdIndex + i + 1) % USART_BUF_SIZE ] == HEADER) )
        {
            break;
        }
    }
    if((i==ucRcvCnt-1) && (gucRxBuffer[ ((unsigned int)ucRdIndex + ucRcvCnt - 1) % USART_BUF_SIZE ] != HEADER))
    {
        i++;
    }       

    //i만� ��터를 버림
    USART0_ucTrashNChar(i);     

    return;
}

The variables that the error stated that they were not declared were all declared under USART0.h file which i have included in the herkulex.cpp file. 错误声明未声明的变量全部在USART0.h文件中声明,而我已将其包含在herkulex.cpp文件中。 May i know what's wrong with the codes? 我可以知道密码有什么问题吗? THanks! 谢谢!

USART0.h code: 
#ifndef USART0_lib
#define USART0_lib

#include <Arduino.h>
#include <Wire.h> 
#include <inttypes.h>
//ìˆ˜ì‹  버í�¼ì�˜ 사ì�´ì¦ˆ ì„ ì–¸
#define USART_BUF_SIZE 256

//ìˆ˜ì‹  버í�¼, ì�½ê¸° ì�¸ë�±ìŠ¤, 쓰기 ì�¸ë�±ìŠ¤, ë�°ì�´í„° 수를 ì €ìž¥í•  변수 ì„ ì–¸
volatile unsigned char gucRxBuffer[USART_BUF_SIZE];
volatile unsigned char gucRxBufferReadIdx;
volatile unsigned char gucRxBufferWriteIdx;
volatile unsigned char gucRxBufferCnt;

#ifdef USART0_cpp
#define EXTERN 
#else 
#define EXTERN extern 
#endif 

class USART0
{
  public: 
  USART0();

  void USART0_Init(unsigned long ulBaudRate);
  void USART0_PutChar(unsigned char ucData);
  void USART0_PutNChar(unsigned char *pucData, unsigned char ucCnt);
  unsigned char USART0_ucGetNChar(unsigned char *pucTarget, unsigned char ucCnt);
  unsigned char USART0_ucTrashNChar(unsigned char ucCnt);
  void USART0_ClearBuffer(void);

//  private :no private
};


#endif

USART0_PutChar is a member function of class USART0 . USART0_PutChar是类USART0的成员函数。 You are trying to call this from another class member function in the same way as standalone a non-member function - You need an instance of USART0 to call the function on. 您尝试以与独立非成员函数相同的方式从另一个类成员函数调用此函数-您需要一个USART0实例才能对其进行调用。

In relation to your comment, you would need to create an instance of USART0 then call the function like you would any other member function 关于您的评论,您需要创建一个USART0实例,然后像调用其他任何成员函数一样调用该函数

 USART0 obj;
 obj.USART0_PutChar();

You cannot call a member function like you are trying to currently unless you are calling it from another member function of the same class . 除非您正在同一类的另一个成员函数进行调用,否则您将无法像您目前尝试的那样调用成员函数。 You might be able to achieve what you want by passing an instance of USART0 (or pointer or reference to one) as an argument to the function in which you need to call USART0_PutChar() . 通过将USART0实例(或指针或引用的实例)作为参数传递给需要在其中调用USART0_PutChar()的函数,您可能能够实现所需的USART0_PutChar() Alternatively you could consider making this function a static member function of USART0 although that may be unsuitable depending on whether or not the function requires access to non-static member variables. 或者,您可以考虑将此函数设为USART0的静态成员函数,尽管根据函数是否需要访问非静态成员变量而可能不合适。

The function USART0_PutNChar (and the others) are member functions of another class. 函数USART0_PutNChar (和其他函数 )是另一个类的成员函数 You need to create an instance of that class and call the functions through this object: 您需要创建该类的实例,然后通过该对象调用函数:

USART0 usart0;

usart0.USART0_PutNChar(...);

However, it seems like the class USART0 doesn't contain any state, which means you can either make the functions global (ie not in a class); 但是,类USART0似乎不包含任何状态,这意味着您既可以使函数成为全局函数(即不在类中);也可以使函数成为全局函数。 Make the functions static, as in 将函数设为静态,如

// Using `struct` as there is no private or protected data/functions
struct USART0
{
    static void USART0_PutNChar(...);
    ...
};

And the third alternative is to make USART0 into a namespace: 第三种选择是使USART0成为命名空间:

namespace USART0
{
    void USART0_PutNChar(...);
    ...
}

In these last two cases you call the functions like this: 在这最后两种情况下,您将调用以下函数:

USART0::USART0_PutNChar(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM