c++ - C++ “was not declared in this scope”编译错误和修改提示

标签 c++ accelerometer arduino atmega

我正在尝试修改此代码,以使其在Arduino Mega上正常工作。我是C的新手,所以我可能犯了一些重大错误。顺便说一下,这是一个自平衡滑板。 :P

此代码取自ATmega32(来自:[url = http://sites.google.com/site/onewheeledselfbalancing/Home/twin-wheel-self-balancing-skateboard-lightweight-version/code4]http://sites.google.com/site/onewheeledsel...t-version/code4[/url],我正在尝试使其在Arduino Mega上运行。

该代码是为ATmega32开发板编写的
http://www.active-robots.com/products/controllr/m32db.shtml

谢谢!

这是我遇到的第一个错误:

In function 'void timer_init()': error: 'TCCR0' was not declared in this scope In function 'int main()':



有人可以解释我怎么了吗?
我几乎是编程的初学者,但我已经阅读了很多书籍/网站,而且学习速度也很快! ^^
这是完整的代码(很长):
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <math.h>

define CLOCK_SPEED 16000000

define OCR1_MAX 1023

typedef unsigned char u8; void set_motor_idle(void); void InitPorts(void); float level=0; float Throttle_pedal; float aa; float accelraw; float x_acc; float accsum; float x_accdeg;

float gyrosum;

float gangleratedeg; float gangleraterads; float ti = 2.2;

float overallgain; float gaincontrol; float batteryvolts = 24; float gyroangledt; float angle; float anglerads; float balance_torque; float softstart;

float cur_speed; float cycle_time = 0.0064; float Balance_point; float a0, a1, a2, a3, a4, a5, a6;//Savitzky-Golay variables for accelerometer

int i; int j; int tipstart; void InitPorts(void) { PORTC=0x00; //Port C pullups set to low (no output voltage) to begin with DDRC=0xFF; //Port C pins all set as output via the port C direction register //PORTC |= (1<

DDRA=0x00; //all port A pins set as input PORTA=0x00; //Port A input pullups set to low pullups

DDRD=0xFF; //Configure all port D pins as output as prerequisite for OCR1A (PinD5) and OCR1B (Pin D4) working properly

PORTB=0x00; //Port B pullups set to low (no output voltage) to begin with DDRB=0xFF; //All port B pins set to output

} /* IO: I am using ATMega32 16MHz with external crystal clock. New planned pin arrangement to OSMC motor controller PC4 Onboard LED PD5/OC1A ALI -> OSMC pin 6 PD4/OC1B BLI -> OSMC pin 8 PC1 Disable -> OSMC pin 4 PC2 BHI -> OSMC pin 7 PC3 AHI -> OSMC pin 5 PA6/ADC6 Vbatt/10 -> OSMC pin 3 PA1/ADC1 pitch rate gyro PA0/ADC0 accelerometer / void adc_init(void) { / turn off analogue comparator as we don't use it / ACSR = (1 << ACD); / select PA0 / ADMUX = 0; ADMUX |=(1< Set ADC prescaler to 128, enable ADC, and start conversion / ADCSRA = 0 | (1< / wait until bogus first conversion finished */ while (ADCSRA & (1 << ADSC)) { } }

uint16_t adc_read(uint8_t channel) {
/* select channel / ADMUX = channel; ADMUX |=(1< start conversion /
ADCSRA |= (1 << ADSC); /
wait until conversion finished / while (ADCSRA & (1 << ADSC)) { } / return the result */ return ADCW; }

/* 156 cycles per sec, 6.4ms per cycle MEASURED ON OSCILLOSCOPE*/ /* read all the ADC inputs and do some conversion */ void sample_inputs(void) {

uint16_t adc0, adc1, adc2, adc3, adc4, adc5;
 gyrosum=0;   adc0 = adc_read(0); /* accelerometer pin PA0 */   accelraw

= (float) adc0; for (j=0; j<7; j++) { adc1 = adc_read(1); //gyro pin PA1 gyrosum = (float) gyrosum + adc1; //using a mean of 7 samples per loop for the gyro so it gets a complete update with each loop of the program }

adc2 = adc_read(2); /* grey wire overallgain (via cutout switch)

position PA2*/ adc3 = adc_read(3); /* Position lever pulled back position PA3*/ adc4 = adc_read(4); /* Throttle_pedal position PA4*/ adc5 = adc_read(5); /* Position lever pushed forwards position PA5*/ //adc6 = adc_read(6); /* Vbatt input from OSMC (not used at present) position PA6*/ //Sav Golay filter for accel only a0 = a1; a1 = a2; a2 = a3; a3 = a4; a4 = a5; a5 = a6; a6 = (float) accelraw; accsum = (float) ((-2*a0) + (3*a1) + (6*a2) + (7*a3) + (6*a4) + (3*a5) + (-2*a6))/21; //Sav Golay calculation

    gaincontrol = (float) gaincontrol*0.9 + 0.1*adc2/341;

//smooths any voltage spikes and gives range 0-3 Throttle_pedal=(float) Throttle_pedal*0.9 + 0.1*adc4/341; //smooths any voltage spikes and gives range 0-3

//Cuts the motor if the dead mans button is let go //(gaincontrol variable also wired in through this button to adc2 if (adc2<100) { Throttle_pedal=0.001; gaincontrol=0.001; } overallgain = gaincontrol*softstart; //what to do if lever pulled back or pushed forwards or not doing anything: Balance_point = 514; if (adc3>100) Balance_point=534;

if (adc5>100) Balance_point=494;

 PORTB |= (1<<PB2);//Port B2 turned on/off once per loop so I can

measure loop time with an oscilloscope

/ACCELEROMETER signal processing/ /Subtract offsets/ x_acc=(float) accsum - Balance_point; //accsum is SG value for accelerometer, not a true "sum" so no need to divide by 7 if (x_acc<-250) x_acc=-250; //cap accel values to a range of -250 to +250 (80 degree tilt each way) if (x_acc>250) x_acc=250; /* Accelerometer angle change is about 3.45 units per degree tilt in range 0-30 degrees(sin theta) Convert tilt to degrees of tilt from accelerometer sensor. Sin angle roughly = angle for small angles so no need to do trigonometry. x_acc below is now in DEGREES*/

x_accdeg= (float) x_acc/-3.45; //The minus sign corrects for a back to front accelerometer mounting!

  /*GYRO signal processing*/
 /*Subtract offsets: Sensor reading is 0-1024 so "balance point"

i.e. my required zero point will be that reading minus 512*/

/Gyro angle change of 20mV per deg per sec from datasheet gives change of 4.096 units (on the scale of 0 - 1023) per degree per sec angle change This limits the rate of change of gyro angle to just less than the maximum rate it is actually capable of measuring (100deg/sec). Note all these fractions are rounded up to an integer later just before it is sent to the PWM generator which in turn is connected to the motor controller/ gangleratedeg=(float)((gyrosum/7) - 508)/4.096; //gyrosum is a sum of a group of 7 samples so divide by 7 for gyro value if (gangleratedeg < -92) gangleratedeg=-92; if (gangleratedeg

92) gangleratedeg=92 /I turn port B2 on and off once per main program cycle so I can attach an oscilloscope to it and work out the program cycle time I use the cycle time to work out gyro angle change per cycle where you have to know the length of this time interval/ PORTB &= (0<



/ ti表示“i”的缩放比例
或积分因子(当前为2.2
这里)gyroangledt是anglechange
自上次以陀螺仪度数为周期以来
传感器,其中ti是比例因子
(理论上应为1,但约为2.2
使板子感觉更紧)
角速度现在以度为单位
每秒aa改变时间
常数,即较小的aa值使
加速度计时间常数更长
慢慢地校正陀螺仪
漂移/

aa = 0.005;
gyroangledt =(float)ticycle_timegangleratedeg;
gangleraterads =(float)gangleratedeg * 0.017453;

/ DEGREES中的新 Angular 是旧 Angular
加上自陀螺的 Angular 变化,因为
最后一轮新
加速度读数因数/ Angular =
( float )((1-aa)*(angle + gyroangledt))
+(aa * x_accdeg); //主 Angular 计算功能* / //转换
从度到弧度的 Angular
 anglerads=(float)angle*0.017453;
      balance_torque=(float)(4.5*anglerads)

+(0.5 * gangleraterads);

cur_speed =(float)(cur_speed +
( throttle 踏板*平衡扭矩*
cycle_time))* 0.999;

/ *级别值是-1至+1,
代表要发送的占空比
到马达。转换为弧度
帮助我们保持在这些限制之内
级别=(balance_torque + cur_speed)*总增益;

}

void timer_init(){TCCR0 = 0 |
(1 <

// PWM模式为“PWM,相位校正,
10位“TCCR1A = 0 |(1 <

(1 <

无效set_motor()

/ *级别术语是级别术语
从-1023调整为+1023
准备发送给PWM电机的整数
依次控制端口
连接到OSMC * / {

//如果(level <-0.9)level =
-0.9; //检查是否在合理范围内//如果(level> 0.9)level = 0.9;

int16_t leveli =
(int16_t)(等级* 1023); //注意我们
取我们有的浮点值
最终获得“等级”,我们乘以
到1023年,然后将其变成
将值输入之前的整数
PWM发生器为“leveli”

如果(leveli <-1020)
leveli = -1020; //我们仔细检查
在合理的PWM限制内,而不是
想突然被甩掉
如果(leveli> 1020)leveli = 1020

/在端口B1上设置LED或蜂鸣器
如果扭矩过大,请警告我减速
交付量超过最大值的50%
可能的原因是
你总是需要一些备用马达
万一您开始给小费
以速度前进如果电动机已经
奔跑,你将要
高速摔倒!有些使用
自动提示返回例程自动
限制最高速度。现在我会做
这样更容易/

如果(level <-0.7 || level> 0.7){
PORTB | =(1 <
PORTB&=(0 <

软启动=( float )软启动+0.001;
如果(softstart> 1.0)softstart = 1.0;
//PORTC |= (0<<PC1);   // AHI=1  PinC3, BHI=1 PinC2 set both to ON for

OSMC可以工作,并且两者都可以关闭以关闭
马达关闭/ *注意:不知道为什么,但是
停止电动机按方向切断
最后我对硬线的改变
AHI和BHI至+ 12V / /禁用
通过将PinC1输出设置为零来进行OSMC,
1将禁用OSMC * / PORTC
| = 0x0c; //使C1下拉
取消禁用OSMC,即启用它。
PORTC&=〜0x02; //如果禁用则关闭
(leveli <0){
OCR1A = -leveli; // ALI是PWM,因为leveli变量是
负符号值,请保持
减号在这里登录! OCR1B = 0; //
BLI = 0}否则{OCR1A = 0; //
ALI = 0前进为leveli
变量是一个正号
OCR1B =等级1; // BLI是PWM}}

int main(void){
InitPorts();

adc_init();

timer_init();

/ *初始倾斜启动代码打开
微型,而板倾斜到一侧,
骑手要踩到它,如果倾斜
Angular 越过零(中)点平衡
算法开始运作
否则永远锁定在此循环中
直到倾斜到水平位置
当骑手上车时* /
tipstart = 0; accelraw = 0;

同时(tipstart <1){

//您需要使用它来允许SG
过滤器收紧到适当的稳定度
首次开机时的值(value),
在查看累积值之前
(下面)。

对于(i = 0; i <20; i ++){
sample_inputs();
}

if(accsum <504 || accsum> 524){//
如果(x_accdeg> 0){tipstart = 0; }
否则{tipstart = 1;
软启动= 0.4; }}

Angular = 0; cur_speed = 0; / *倾斜结束
起始代码。如果超出这一点
然后机器就变成水平了
活性*/

sei();

while(1) {sample_inputs();

set_motor();

}}

最佳答案

您很可能为您的构建指定了错误的MCU。 DDRA在Arduino Mega的ATmega1280上存在,而DDRA在常规Arduino的ATmega328上不存在。

如果您使用的是Arduino UI,请转到工具|登上并选择Arduino Mega。

如果您使用自己的构建系统,则需要更新在gcc命令行中为-mmcu =指定的值。

关于c++ - C++ “was not declared in this scope”编译错误和修改提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972475/

相关文章:

android - 将加速度计数据从设备坐标转换为真实世界坐标

iphone - iOS 理解 iPhone 在自由落体中的加速度计数据

c++ - 编译Arduino代码(NodeMCU)时出错

c++ - 在 C++03 中将字符串转换为 int

c++ - 汇编语言扫描长字符串以计算元音的数量

Android在地球坐标系上获取加速度计

java - 处理 IDE 无法直接从串行读取数据

c++ - 如何从控制台启动可执行文件并使用Qt读取输出

C++在同一行读取具有多种类型的文件

c++ - 如何使用 gcc 内联汇编代码访问成员变量