//------------------------------------------------------------
int xPin = 2; // select the input pin for the potentiometer
int gyroPin = 1;
int steerPin = 3;
int ledPin = 13; // select the pin for the LED
int pwmPinL = 9;
int pwmPinR = 10;
int enPin = 7;
float angle = 0;
float angle_old = 0;
float angle_dydx = 0;
float angle_integral = 0;
float balancetorque = 0;
float rest_angle = 0;
float currentspeed = 0;
int steeringZero = 0;
int steering = 0;
int steeringTemp = 0;
float p = 8; //2
float i = 0; //0.005
float d = 1300; //1000
float gyro_integration = 0;
float xZero = 0;
int gZero = 445; //this is always fixed, hence why no initialisation routine
unsigned long time, oldtime;
int pwmL;
int pwmR;
boolean over_angle = 0;
void setup() {
unsigned int i = 0;
unsigned long j = 0; //maximum possible value of j in routine is 102300 (100*1023)
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(115200);
analogReference(EXTERNAL);
//----------------------------------------------------
TCCR1B = TCCR1B & 0b11111000 | 0x01;
analogWrite(pwmPinL,127);
analogWrite(pwmPinR,127);
digitalWrite(enPin,HIGH);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
//-----------------------------------------------------
delay(100);
for (i = 0; i < j =" j" steeringzero =" analogRead(steerPin);" xzero =" j/100;" oldtime =" micros();" time =" micros();">= (oldtime+5000)){
oldtime = time;
calculateAngle();
steering = (analogRead(steerPin) - steeringZero)/(15+(abs(angle)*8));
//-----OVER ANGLE PROTECTION-----
if (angle > 20 || angle < -20) { digitalWrite(enPin,HIGH); over_angle = 1; delay(500); } //-----END----- if (over_angle) { //if over_angle happened, give it a chance to reset when segway is level if (angle <> -1) {
digitalWrite(enPin, LOW);
over_angle = 0;
}
}
else {
//-----calculate rest angle-----
if (currentspeed > 10)
{
rest_angle = 0;
//-----END-----
angle_integral += angle;
balancetorque = ((angle+rest_angle)*p) + (angle_integral*i) + (angle_dydx*d);
angle_dydx = (angle - angle_old)/200; //now in degrees per second
angle_old = angle;
currentspeed += (balancetorque/200);
pwmL = (127 + balancetorque + steering);
//-----COERCE-----
if (pwmL < pwml =" 0;"> 255)
pwmL = 255;
//-----END-----
pwmR = (127 - balancetorque + steering);
//-----COERCE-----
if (pwmR < pwmr =" 0;"> 255)
pwmR = 255;
//-----END-----
analogWrite(pwmPinL, pwmL);
analogWrite(pwmPinR, pwmR);
}
}
}
void calculateAngle() {
//Analogref could be as small as 2.2V to improve step accuracy by ~30%
//uses small angle approximation that sin x = x (in rads). maybe use arcsin x for more accuracy?
//analogref is off the gyro power supply voltage, and routine is calibrated for 3.3V. maybe run acc/gyro/ref off 1 3.3V regulator, an
//accurately measure that.
//routine runs at 200hz because gyro maximum response rate = 200hz
float acc_angle = 0;
float gyro_angle = 0;
acc_angle = (((analogRead(xPin)-xZero)/310.3030)*(-57.2958);
gyro_angle = ((analogRead(gyroPin) - gZero)*4.8099)/200;
gyro_integration = gyro_integration + gyro_angle; //integration of gyro and gyro angle calculation
angle = (gyro_integration * 0.99) + (acc_angle * 0.01); //complementary filter
gyro_integration = angle; //drift correction of gyro integration
}
Monday, 2 November 2009
Arduino code
I see some of you are interested in the code.. well here it is, unedited:
Subscribe to:
Post Comments (Atom)
any chance to get you to post schematics.....?
ReplyDeleteThere appears to be an issue in the syntax of the for loop which follows delay(100);
ReplyDeletesomething mistyped in the code after delay(100);
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHey,
ReplyDeleteYou inspired me into wanting to build one myself. Any chance of putting up a post with any resources you used? Also is it possible to get a contact email to ask questions to help me get started?
Thanks
Hey,
ReplyDeleteI was just poking through the code and wondered, what does pin 7, the enPin, do? Is it an external enable or inside the program loop and I just missed it? Thanks, and great build! I hope mine is as stable as yours!
Hello! iam From Peru! iam in the Catolica University and myself and my group are building a segway! well... trying haha we had build our own H bridge controllers with power mosftes , also we had to do our chasis , its very simillar tu yours, also we had a gear box ( we are using a 1/2 hp motr from robotmarketplace) maybe i will post some pics later! write my email pls! cesar.sasaki@gmail.com!
ReplyDeleteSomehow, this code has been mangled, and won't compile. The loop() function is completely missing (kind of an important part of an arduino program), and I'm scratching a hole in my head trying to figure out what the quoted portions are supposed to be.
ReplyDeleteI have a robot with a very similar setup and I wanted to try to adapt this code, but there are a few pieces (aside from those that aren't even valid code) that are a bit confusing. A circuit diagram to go with it would go a long way, or at least a description of which arduino pins are connected to what. For instance, I don't see anything that looks like a control for the direction of the motors.
I just figured it out, the quotes are where blogger saw a greater-than sign followed eventually by a less-than sign, thought that it was meant to be an html tag, and tried to turn each assignment operation into an attribute name and value, horribly mangling the code and apparently deleting some important parts of it. Perhaps if you post it between <code> and </code>, the parser will leave it alone. I've posted php that way on blogger before, and it came out alright.
ReplyDeleteHey, I was just wondering if you could repost the code with the void loop() and whatever is deleted here. I tried playing around with the posted code in the Arduino IDE interface, but can't get it working.
ReplyDeleteThat, or if your really nice you could send it to my email? (That's vexxir(a)hotmail.com), thanks!
Yeah, I would like to see the cleaned up code too, I'm working on something similar for a robot.
ReplyDeleteIt may be that your sole purpose in life is simply to serve as a warning to others. ....................................................
ReplyDelete激情成人聊天室情色成人辣妹胸部辣妹視訊露奶辣妹自拍三點全露內衣秀台灣成人貼圖成人電影院三點全裸免費視訊辣妹av圖裸體自拍色情聊天美女視訊g點色情訊息淫女火辣av辣妹圖片免費視訊聊天室性情色天空調情上床圖片裸體自拍走光照片走光視訊情色成人18成人區火辣美女成人vcd成人影片下載本土av性愛情慾淫妹美女聊天性愛聊天室女生自慰影片免費看a圖淫婦巨乳辣妹視訊成人女生自慰方法免費情色限制級a片穿幫情色下載情色網站
ReplyDeleteHello,這裡真是百看不厭的部落格 .........................................
ReplyDelete我愛78論壇影片我愛78論壇 寶貝視訊色情自拍寶貝視訊ok論壇影音聊天fm358影音聊天室hibb影音視訊fm1768影音情人趣味影音交友聊天室影音交友lover99新竹援交a片免費線上看新竹援交成人影片情色網辣妹視訊後宮視訊交友後宮視訊聊天一夜情援交妹視訊104免費成人情色文學小說免費成人影片台灣kiss情色貼圖av成人網無碼av女優爆乳娘影片寶貝視訊色情自拍寶貝視訊ok論壇影音聊天fm358影音聊天室hibb影音視訊fm1768影音情人趣味影音交友聊天室影音交友lover99後宮電影電影院a片天堂無碼影片 美女sexy激情網愛聊天 介紹免費觀賞78論壇080情人網亞洲東洋影片sexy girl亞洲東洋影片gay片免費下載
ReplyDelete^^ 謝謝你的分享,祝你生活永遠多彩多姿!........................................
ReplyDelete我們唯一需要恐懼的事,是恐懼本身........................................
ReplyDelete一個人就像一個分數,他的實際才能是分子,他對自己的評價是分母。分母越大,則分數的價值越小。 ....................................................
ReplyDelete朝著既定的目標走,就不會迷失。........................................
ReplyDeleteCustom makes all things easy~~加油哦.............................................
ReplyDelete不要把生命看得太嚴肅,反正我們不會活著離開。..................................................
ReplyDelete成人電影線上看 ut男視訊 聊天事ut 免費a觀看 絲襪同志交友 av女優性交影影片 性感褲襪美女寫真 免費影片網 色情自拍偷拍 愛的聊天室 女女愛愛 av情趣用品 18卡通漫畫 少女自拍網 熊貓影城 a片圖 情色卡漫 a色情影片 一夜情 383 完美情人視訊網站 玩美女人 免費影片 正妹辣妹 人妻遊戲 熟女 免費視訊 視訊聊天室 a圖片a 視訊聊天 85cc 微風成人交友區 嘟嘟 777 小魔女自拍貼圖 台灣援交妹 情人視訊 辣妹脫衣 av影片 洪爺影城 大奶熟女 sexy情色 a片 巨乳 性愛情慾貼圖 後宮電影院 jack 限制級 色情電話0204 激情性愛 明星走光
ReplyDelete一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤......................................................
ReplyDeletecan you post the right code please?
ReplyDeleteVery interested in the Final code, You could try and upload to Github or even the Arduino sketch file to a server. Would love to get a hold of this, I have been trying to work out a project like this for a while.
ReplyDeleteJohn
j.hobson AT westnet DOT com DOT au
你不能決定生命的長度,但你可以控制它的寬度..................................................
ReplyDelete從人生中拿走友誼,猶如從生活中移走陽光 ............................................................
ReplyDeleteyou two make a lovely couple!............................................................
ReplyDelete幸福不是一切,人還有責任。....................................................................
ReplyDelete在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................
ReplyDeletePen and ink is wits plough. ....................................................................
ReplyDelete成熟,就是有能力適應生活中的模糊。.................................................................
ReplyDelete欣賞你的內容是美德~~支持你是最大回應..................................................................
ReplyDeleteyou are the best 1..................................................................
ReplyDelete願你心情如秋高氣爽!笑臉如鮮花常開!..................................................................
ReplyDelete文章是心情的反應~~祝妳天天寫的都是讓人開心的好文章哦!!............................................................
ReplyDelete加油!充實內函最重要!Beauty is but skin- deep...................................................................
ReplyDelete如果你批評他人。你就沒有時間付出愛............................................................
ReplyDelete你不能左右天氣,但你可以改變心情............................................................
ReplyDelete不會從失敗中找尋教訓的人,成功之路是遙遠的。.................................................
ReplyDelete享受你自己的生活,不要與他人相比。............................................................
ReplyDelete這不過是滑一跤,並不是死掉而爬不起來了。..................................................
ReplyDelete一棵樹除非在春天開了花,否則難望在秋天結果。..................................................
ReplyDeleteA good medicine tastes bitter. ............................................................
ReplyDeleteLook before you leap.................................................
ReplyDeletePoverty tries friends...................................................................
ReplyDelete我是天山,等待一輪明月。......................................................................
ReplyDelete河水永遠是相同的,可是每一剎那又都是新的。..................................................
ReplyDelete喜歡看大家的文章,每篇都是一個故事,都是一種心情~~祝大家開心愉快...............................................................
ReplyDelete在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」..................................................
ReplyDelete來逛逛blog~~跟您打聲招呼............................................................
ReplyDeleteCmon bro at least give us the code in a notepad file or something. I really need it.
ReplyDeletecan u upload the wiring setup??
ReplyDeletei found a mistake at the second IF HERE:
//-----OVER ANGLE PROTECTION-----
if (angle > 20 || angle < -20) { digitalWrite(enPin,HIGH); over_angle = 1; delay(500); } //-----END----- if (over_angle) { //if over_angle happened, give it a chance to reset when segway is level if (angle <> -1) {
digitalWrite(enPin, LOW);
over_angle = 0;
}
}
else {
Id love to see a wiring diagram, this is really nice
ReplyDelete