c語言rbf編程,rb在c語言

本文目錄一覽:

急!!!RBF的PID控制的C程序,最好可以在單片機上實現的那種,懸賞還會追加的

PIC單片機的你看一下

//*********************************************************************************

#include pic.h

#include pic16f684.h

#include math.h

#include stdlib.h

void Init();

void PID();

void Set_Constants();

bit flag1,do_PID,int_flag;

signed char en0, en1, en2, en3, term1_char, term2_char, off_set;

unsigned char temp;

short int temp_int;

unsigned short int ki, kd, kp;

signed int SumE_Min, SumE_Max, SumE, integral_term, derivative_term, un;

signed long Cn;

// __CONFIG _CP_OFF _CPD_OFF _BOD_OFF _MCLRE_ON _WDT_OFF _INTRC_OSC_NOCLKOUT _FCMEN_ON

//***************************************************************************

// Positional PID 256 Hz

//***************************************************************************

//***************************************************************************

//Main() – Main Routine

//***************************************************************************

void main()

{

Init(); //Initialize 12F629 Microcontroller

Set_Constants(); //Get PID coefficients ki, kp and kd

while(1) //Loop Forever

{

if(do_PID){

PID();

}

}

}

//***************************************************************************

//Init – Initialization Routine

//***************************************************************************

void Init()

{

PORTA = 0;

TRISA = 0b00101101; // Set RA4 and RA2 as outputs

PORTC = 0;

TRISC = 0b00000011; // Set RC0 and RC1 as inputs, rest outputs

CMCON0 = 0x07; // Disable the comparator

IRCF0 = 1; // Used to set intrc speed to 8 MHz

IRCF1 = 1; // Used to set intrc speed to 8 MHz

IRCF2 = 1; // Used to set intrc speed to 8 MHz

CCP1CON = 0b01001100; // Full bridge PWM forward

ECCPAS = 0; // Auto_shutdown is disabled for now

PR2 = 0x3F; // Sets PWM Period at 31.2 kHz

T2CON = 0; // TMR2 Off with no prescale

CCPR1L = 0; // Sets Duty Cycle to zero

TMR2ON = 1; // Start Timer2

ANSEL = 0b00110101; // Configure AN0,AN2,AN4 and AN5 as analog

VCFG = 0; // Use Vdd as Ref

ADFM = 1; // Right justified A/D result

ADCS0 = 1; // 16 TOSC prescale

ADCS1 = 0;

ADCS2 = 1;

CHS0 = 0; // Channel select AN0

CHS1 = 0;

CHS2 = 0;

ADON = 1; //Turn A/D on

en0 = en1 = en2 = en3 = term1_char = term2_char =0;

ki = kd = 0;

kp = off_set = 0;

temp_int = integral_term = derivative_term = un =0;

SumE_Max = 30000;

SumE_Min = 1 – SumE_Max;

do_PID = 1; // Allowed to do PID function

T0CS = 0; // Timer0 as timer not a counter

TMR0 = 10; // Preload value

PSA = 0; // Prescaler to Timer0

PS0 = 0; // Prescale to 32 = 256 Hz

PS1 = 0;

PS2 = 1;

INTCON = 0;

PIE1 = 0;

T0IE = 1; // Enable Timer0 int

GIE = 1;

return;

}

void PID() // The from of the PID is C(n) = K(E(n) + (Ts/Ti)SumE + (Td/Ts)[E(n) – E(n-1)])

{

integral_term = derivative_term = 0;

// Calculate the integral term

SumE = SumE + en0; // SumE is the summation of the error terms

if(SumE SumE_Max){ // Test if the summation is too big

SumE = SumE_Max;

}

if(SumE SumE_Min){ // Test if the summation is too small

SumE = SumE_Min;

} // Integral term is (Ts/Ti)*SumE where Ti is Kp/Ki

// and Ts is the sampling period

// Actual equation used to calculate the integral term is

// Ki*SumE/(Kp*Fs*X) where X is an unknown scaling factor

// and Fs is the sampling frequency

integral_term = SumE / 256; // Divide by the sampling frequency

integral_term = integral_term * ki; // Multiply Ki

integral_term = integral_term / 16; // combination of scaling factor and Kp

// Calculate the derivative term

derivative_term = en0 – en3;

if(derivative_term 120){ // Test if too large

derivative_term = 120;

}

if(derivative_term -120){ // test if too small

derivative_term = -120;

} // Calculate derivative term using (Td/Ts)[E(n) – E(n-1)]

// Where Td is Kd/Kp

// Actual equation used is Kd(en0-en3)/(Kp*X*3*Ts)

derivative_term = derivative_term * kd; // Where X is an unknown scaling factor

derivative_term = derivative_term 5; // divide by 32 precalculated Kp*X*3*Ts

if(derivative_term 120){

derivative_term = 120;

}

if(derivative_term -120){

derivative_term = -120;

}

// C(n) = K(E(n) + (Ts/Ti)SumE + (Td/Ts)[E(n) – E(n-1)])

Cn = en0 + integral_term + derivative_term; // Sum the terms

Cn = Cn * kp / 1024; // multiply by Kp then scale

if(Cn = 1000) // Used to limit duty cycle not to have punch through

{

Cn = 1000;

}

if(Cn = -1000)

{

Cn = -1000;

}

if(Cn == 0){ // Set the speed of the PWM

DC1B1 = DC1B1 = 0;

CCPR1L = 0;

}

if(Cn 0){ // Motor should go forward and set the duty cycle to Cn

P1M1 = 0; // Motor is going forward

temp = Cn;

if(temp^0b00000001){

DC1B0 = 1;

}

else{

DC1B0 = 0;

}

if(temp^0b00000010){

DC1B1 = 1;

}

else{

DC1B1 = 0;

}

CCPR1L = Cn 2; // Used to stop the pendulum from continually going around in a circle

off_set = off_set +1; // the offset is use to adjust the angle of the pendulum to slightly

if(off_set 55){ // larger than it actually is

off_set = 55;

}

}

else { // Motor should go backwards and set the duty cycle to Cn

P1M1 = 1; // Motor is going backwards

temp_int = abs(Cn); // Returns the absolute int value of Cn

temp = temp_int; // int to char of LS-Byte

if(temp^0b00000001){

DC1B0 = 1;

}

else{

DC1B0 = 0;

}

if(temp^0b00000010){

DC1B1 = 1;

}

else{

DC1B1 = 0;

}

CCPR1L = temp_int 2; // Used to stop the pendulum from continually going around in a circle

off_set = off_set -1;

if(off_set -55){

off_set = -55;

}

}

en3 = en2; // Shift error signals

en2 = en1;

en1 = en0;

en0 = 0;

do_PID = 0; // Done

RA4 = 0; // Test flag to measure the speed of the loop

return;

}

void Set_Constants()

{

ANS2 = 1; // Configure AN2 as analog

ANS4 = 1; // Configure AN4 as analog

ANS5 = 1; // Configure AN5 as analog

ADFM = 1; // Right justified A/D result

CHS0 = 0; // Channel select AN4

CHS1 = 0;

CHS2 = 1;

temp = 200; // Gives delay

while(temp){

temp–;

}

GODONE = 1;

while(GODONE);{

temp = 0; // Does nothing…..

}

ki = ADRESH 8; // Store the A/D result to Integral Constant

ki = ki + ADRESL;

CHS0 = 1; // Channel select AN5

CHS1 = 0;

CHS2 = 1;

temp = 200; // Gives delay

while(temp){

temp–;

}

GODONE = 1;

while(GODONE);{

temp = 0; // Does nothing…..

}

kd = ADRESH 8; // Store the A/D result to Differential Constant

kd = kd + ADRESL;

CHS0 = 0; // Channel select AN2

CHS1 = 1;

CHS2 = 0;

temp = 200; // Gives delay

while(temp){

temp–;

}

GODONE = 1;

while(GODONE);{

temp = 0; // Does nothing…..

}

kp = ADRESH 8; // Store the A/D result to Proportional Constant

kp = kp + ADRESL;

CHS0 = 0; // Channel select AN0

CHS1 = 0;

CHS2 = 0;

}

void interrupt Isr()

{

if(T0IFT0IE){

TMR0 = 10; // Preload value

T0IF = 0; // Clear Int Flag

// flag1 = (!flag1);

RA4 = 1;

temp_int = 0;

temp_int = ADRESH 8; // Store the A/D result with offset

temp_int = temp_int + ADRESL – 512;

en0 = temp_int + off_set/8; // Store to error function asuming no over-flow

do_PID = 1; // Allowed to do PID function

GODONE = 1; // Start next A/D cycle

}

else

{

PIR1 = 0;

RAIF = 0;

INTF = 0;

}

if(temp_int 180){ //Check if error is too large (positive)

DC1B0 = DC1B1 = 0; // Stop PWM

CCPR1L = 0;

en0 = en1 = en2 = en3 = term1_char = term2_char = off_set = 0; // Clear all PID constants

Cn = integral_term = derivative_term = SumE = RA4 = 0;

do_PID = 0; // Stop doing PID

}

if(temp_int -180){ //Check if error is too large (negative)

DC1B0 = DC1B1 = 0; // Stop PWM

CCPR1L = 0;

en0 = en1 = en2 = en3 = term1_char = term2_char = off_set = 0; // Clear all PID constants

Cn = integral_term = derivative_term = SumE = RA4 = 0;

do_PID = 0; // Stop doing PID

}

}

關於PIC的C語言編程中定時器方面的資料,哪裡有啊?求關於定時器的一個詳細的例子。

網上有一個名為《PIC16F877單片機編程實例教程》的電子文檔,PDF格式的。這裡有PIC16F877的定時器的C語言樣例程序。如果找不到,留下郵箱號可以給你傳。

給你一個我最近寫的PIC16F886的定時器程序,只要在main函數里調用init_T1()就能操作定時器1:

void init_T1(void) //初始化定時器1

{

TMR1H = 0xF4;TMR1L = 47; //定時3mS

T1CON = 0; //初始化T1

TMR1IE = 1; //開定時器中斷

INTCON = 0XC0; //開總中斷和PEIE外設中斷

TMR1ON = 1;

}

void interrupt T1(void)

{

if(TMR1IF)

{ TMR1IF = 0;

INTCON = 0; //關中斷清標誌位

TMR1IE = 0;

rbf = 1; //具體操作

}

}

但你要先清楚:PIC單片機有很多種類的,雖然指令和架構都一樣。但在某些功能上有區別的。比如上述的877和886的T1定時器帶門控功能,而PIC16F716的T1則不帶門控功能。有的PIC單片機甚至沒有T1和T2定時器。這都需要你自己去看PIC單片機對應的數據手冊(也是PDF格式,在PIC的生產商MICROCHIP的網站上有中文版下載)

用c語言編寫RBF神經網絡程序

RBF網絡能夠逼近任意的非線性函數,可以處理系統內的難以解析的規律性,具有良好的泛化能力,並有很快的學習收斂速度,已成功應用於非線性函數逼近、時間序列分析、數據分類、模式識別、信息處理、圖像處理、系統建模、控制和故障診斷等。

簡單說明一下為什麼RBF網絡學習收斂得比較快。當網絡的一個或多個可調參數(權值或閾值)對任何一個輸出都有影響時,這樣的網絡稱為全局逼近網絡。由於對於每次輸入,網絡上的每一個權值都要調整,從而導致全局逼近網絡的學習速度很慢。BP網絡就是一個典型的例子。

如果對於輸入空間的某個局部區域只有少數幾個連接權值影響輸出,則該網絡稱為局部逼近網絡。常見的局部逼近網絡有RBF網絡、小腦模型(CMAC)網絡、B樣條網絡等。

附件是RBF神經網絡的C++源碼。

C語言複製文件

不應對非文本文件使用fgetc等易受干擾的函數,建議用fread,fwrite讀寫二進制文件

#include

“stdio.h”

/*

保護硬盤,絕對不要一個字節一個字節複製

*/

#define

SIZEOFBUFFER

256*1024L

/*

緩衝區大小,默認為256KB

*/

long

filesize(FILE

*stream)

{

long

curpos,

length;

curpos

=

ftell(stream);

fseek(stream,

0L,

SEEK_END);

length

=

ftell(stream);

fseek(stream,

curpos,

SEEK_SET);

return

length;

}

int

copyfile(const

char*

src,const

char*

dest)

{

FILE

*fp1,*fp2;

int

fsize,factread;

static

unsigned

char

buffer[SIZEOFBUFFER];

fp1=fopen(src,”rb”);

fp2=fopen(dest,”wb+”);

if

(!fp1

||

!fp2)

return

0;

for

(fsize=filesize(fp1);fsize0;fsize-=SIZEOFBUFFER)

{

factread=fread(buffer,1,SIZEOFBUFFER,fp1);

fwrite(buffer,factread,1,fp2);

}

fclose(fp1);

fclose(fp2);

return

1;

}

int

main()

{

copyfile(“file1.txt”,”file2.txt”);

return

0;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/300951.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-29 14:18
下一篇 2024-12-29 14:18

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論