デザインを変更しました.機能は前回と同じです.
背景画を使うことにしました.以下の画像を,pythonのプログラムと同じフォルダにいれてください.
まずはAVRマイコンに以下のプログラムを入力します.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include<avr/io.h> #include<avr/interrupt.h> int G=0; long x=0,y=0,v=0,w=0,p=0,q=0,k=0; int xH=0,xL=0,yH=0,yL=0,vH=0,vL=0,wH=0,wL=0,pH=0,pL=0,qH=0,qL=0; void AVR_init(void) { DDRA=0b11111111; DDRB=0b11111111; DDRC=0b11111111; DDRD=0b11111111; DDRE=0b11111110; DDRF=0b11111111; DDRG=0b11111111; } void USART_init(void) { unsigned int baud; baud=8000000/16/38400-1;//ボーレートの計算 UBRR0H = (unsigned char)(baud>>8);//ボーレート上位 UBRR0L = (unsigned char)baud; //ボーレート下位 UCSR0C = (1<<USBS0)|(3<<UCSZ00); UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);//送受信可 & 受信割り込み許可 } void Utx(int data) //マイコンからデータを送信する { while ( !(UCSR0A & (1<<UDRE0)) ); UDR0 = data; } unsigned char Urx(void) //マイコンがデータを受信する { while ( !(UCSR0A & (1<<RXC0)) ); return UDR0; } void xbee(long a) { Utx(0X30+a/100.); //0X30='0' a%=100; Utx(0X30+a/10.); a%=10; Utx(0X30+a); Utx(0x0a); //LF Utx(0x0d); //CR } void time1_init(void) //time1設定 { TCCR1A =0B00000000; TCCR1B =0B00001010; //8分周 OCR1A =10000; //0.01s間隔割り込み } ISR(USART0_RX_vect) { int RX_num = UDR0; if(RX_num == 's') { G=1; x=0,y=0,v=0,w=0,p=0,q=0; } } ISR(TIMER1_COMPA_vect) //タイマ割り込み { //要素1 x += 1; y += 1; k = x; xH = (k>>8); xL = k; k = y; yH = (k>>8); yL = k; Utx('u');Utx(xH);Utx(xL); Utx('i');Utx(yH);Utx(yL); //要素2; v += 1; w += 5; k = v; vH = (k>>8); vL = k; k = w; wH = (k>>8); wL = k; Utx('v');Utx(vH);Utx(vL); Utx('w');Utx(wH);Utx(wL); //要素3 p += 1; q += 10; k = p; pH = (k>>8); pL = k; k = q; qH = (k>>8); qL = k; Utx('p');Utx(pH);Utx(pL); Utx('q');Utx(qH);Utx(qL); } int main(void) { AVR_init(); USART_init(); time1_init(); sei(); TIMSK = (0<<OCIE1A); //タイマ割り込み拒否 //受信されるまで待つ for(;;) { if(G == 1){break;} Utx('1'); } TIMSK = (1<<OCIE1A); //タイマ割り込み許可 for(;;){ int RX_num = Urx(); if(RX_num == 's') { x=0,y=0,v=0,w=0,p=0,q=0; } } } |
次にIDLEに以下のプログラムを入力します.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#imege background_image = 'background.png' import pygame from pygame.locals import * from sys import exit import time import serial #serial_setup com03 =serial.Serial(2,baudrate=38400) #variable screen_width = 1200 screen_height = 700 height = 510 width = 740 offset = 100 memory = 20 radius = 2 #color_setup BabyBlues =(48,126,169) BabyBlues2 =(28,106,149) LittleBooties =(118,180,219) LittleBooties2=(28,90,129) ABoy =(192,222,246) White =(255,255,255) AGirl =(246,162,183) co=25 Jaded1 =(87,192,132) Jaded2 =(87+co,192+co,132+co) Pumpkin1 =(224,110,2) Pumpkin2 =(224+co,110+co,2+co) purple1 =(153+co,52+co,106+co) purple2 =(153,52,106) #pygame_init pygame.init() screen = pygame.display.set_mode((screen_width,screen_height),0,32) background = pygame.image.load(background_image).convert() font = pygame.font.SysFont('raavi',45) fonts = pygame.font.SysFont('raavi',20) text1 = fonts.render("0",True,White,0) text2 = font.render("X",True,White,0) text3 = font.render("Y",True,White,0) text4 = fonts.render("66000",True,White,0) text5 = fonts.render("10000",True,White,0) text12 = fonts.render("X",True,White,0) text13 = fonts.render("Y",True,White,0) #element_name text8 = font.render("E1",True,White,0) text9 = font.render("E2",True,White,0) text10 = font.render("E3",True,White,0) text11 = font.render("RESET",True,White,0) #start for i in range(1,501): com03.write('s') #roop1 while 1: break_num = 1 #variable_init u,uH,uL,i,iH,iL,v,vH,vL,w,wH,wL,p,pH,pL,q,H,qL= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 T=0 #display_draw_and_update screen.blit(background,(0,0)) screen.blit(text1,(119,600)) screen.blit(text12,(523,617)) screen.blit(text2,(screen_width-220,60)) screen.blit(text13,(98,310)) screen.blit(text3,(screen_width-220,135)) screen.blit(text4,(offset-30,offset-memory)) screen.blit(text5,(861,609)) screen.blit(text8,(screen_width-220,230)) screen.blit(text9,(screen_width-220,327)) screen.blit(text10,(screen_width-220,425)) screen.blit(text11,(screen_width-205,screen_height-105)) pygame.draw.rect(screen,LittleBooties2,Rect((150,90),(740,510))) pygame.draw.rect(screen,LittleBooties,Rect((154,94),(732,502))) for i in range(1,5): pygame.draw.line(screen,LittleBooties2,(154,94+100.4*i),(160,94+100.4*i),4) pygame.draw.line(screen,LittleBooties2,(154+732-7,94+100.4*i),(154+732,94+100.4*i),4) for i in range(1,8): pygame.draw.line(screen,LittleBooties2,(154+91.5*i,94),(154+91.5*i,100),4) pygame.draw.line(screen,LittleBooties2,(154+91.5*i,94+502-7),(154+91.5*i,94+502),4) #element_draw pygame.draw.circle(screen,Jaded1,(1096,263),25) pygame.draw.circle(screen,Pumpkin1,(1096,363),25) pygame.draw.circle(screen,purple1,(1096,458),25) pygame.draw.circle(screen,Jaded2,(1096,263),22) pygame.draw.circle(screen,Pumpkin2,(1096,363),22) pygame.draw.circle(screen,purple2,(1096,458),22) pygame.display.update() #roop2 while break_num: for event in pygame.event.get(): if event.type == QUIT: com03.close() exit() if event.type == MOUSEBUTTONDOWN and event.button == 1: mouse2 = pygame.mouse.get_pos() mouse_x2 = mouse2[0] mouse_y2 = mouse2[1] if (1161>=mouse2[0]>=960)and(655>=mouse2[1]>=592): pygame.draw.rect(screen,BabyBlues,Rect((screen_width-205,screen_height-105),(140,55))) pygame.display.update(Rect((screen_width-205,screen_height-105),(201,63))) for i in range(1,501): com03.write('s') if event.type == MOUSEBUTTONUP and event.button == 1: text11 = font.render("RESET",True,White,0) screen.blit(text11,(screen_width-205,screen_height-105)) pygame.display.update(Rect((screen_width-205,screen_height-105),(140,55))) if (1161>=mouse2[0]>=960)and(655>=mouse2[1]>=592): break_num = 0 #mouse_pos mouse = pygame.mouse.get_pos() mouse_x = mouse[0] mouse_y = mouse[1] mouse_x = (mouse_x-154.)*13.6 pygame.draw.rect(screen,BabyBlues,Rect((screen_width-180,60),(115,145))) text6 = fonts.render("%i"%(int(mouse_x)),True,White,0) text7 = fonts.render("%i"%(131*(-mouse_y+94+502)),True,White,0) screen.blit(text6,(screen_width-160,80)) screen.blit(text7,(screen_width-160,155)) pygame.display.update(Rect((screen_width-180,60),(115,145))) #circle_draw if T > 60: pygame.draw.circle(screen,Jaded1,(int(u)+154,94+502-i/132),radius+1) pygame.draw.circle(screen,Pumpkin1,(int(v)+154,94+502-w/132),radius+1) pygame.draw.circle(screen,purple1,(int(p)+154,94+502-q/132),radius+1) pygame.draw.circle(screen,Jaded2,(int(u)+154,94+502-i/132),radius) pygame.draw.circle(screen,Pumpkin2,(int(v)+154,94+502-w/132),radius) pygame.draw.circle(screen,purple2,(int(p)+154,94+502-q/132),radius) if 732>= u: pygame.display.update(Rect((int(u)+154-5,94+502-i/132-5),(10,10))) if 732>= v: pygame.display.update(Rect((int(v)+154-5,94+502-w/132-5),(10,10))) if 732>= p: pygame.display.update(Rect((int(p)+154-5,94+502-q/132-5),(10,10))) T += 1 #plot_pos num = com03.read(1) if num == 'u': num = com03.read(1) uH = ord(num) num = com03.read(1) uL = ord(num) u = ((uH<<8) + uL) /13.6 elif num == 'i': num = com03.read(1) iH = ord(num) num = com03.read(1) iL = ord(num) i = (iH<<8) + iL elif num == 'v': num = com03.read(1) vH = ord(num) num = com03.read(1) vL = ord(num) v = ((vH<<8) + vL) /13.6 elif num == 'w': num = com03.read(1) wH = ord(num) num = com03.read(1) wL = ord(num) w = (wH<<8) + wL elif num == 'p': num = com03.read(1) pH = ord(num) num = com03.read(1) pL = ord(num) p = ((pH<<8) + pL) /13.6 elif num == 'q': num = com03.read(1) qH = ord(num) num = com03.read(1) qL = ord(num) q = (qH<<8) + qL |
マイコン側の電源を入れて,PCに接続したXbeeの受信が確認できたらIDLEのプログラムを実行します.
このような画面がでてきたら成功です.
以上でリアルタイムグラフ作成は終わりです.
応用編として,このリアルタイムで表示できるグラフを使って,9軸センサの扱い方の紹介をしていますので,よかったら見てください.
4章-16 9軸センサをI2cで使いこなそう1(LSM9DS0)