My Toyota

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

My Toyota

Post by Henko »

Code: Select all

dim notes$(12),gear(6)
app_init
t=0 ! dt=.1 ! v=.1 ! s=0 ! alfa=0 ! u=0 !thr=0 ! gr=0 ! n=0
loop1: slowdown
  if bp("start") then
    thr=0.05 ! slider "throttle" value thr ! p=5
    else ! goto loop1
    end if
loop:
  t+=dt
  if sc("throttle") then 
    p=100*slider_value("throttle")
    if gr=0 then ntar=3.7*p-3.6
    end if
  if bp("up") and gr<5 then ! gr+=1 ! gearbox(gr) ! end if
  if bp("down") and gr>0 then ! gr-=1 ! gearbox(gr) ! end if

  if gr=0 then
    n=.5*ntar+.5*n ! rev=.6*n+rnd(2)-1 ! power=0 ! db=0.00025*dt
    else
    n=min(90,gear(gr)*v)
    rev=.6*n
    power=(p-0.7)*n-0.27*n^2
    db=0.0000011*power*dt
    end if

  kv=power/v
  if v>0 then fric=.02*cos(alfa) else fric=0
  drag=1200*(sin(alfa)+fric)+0.044*(v+u)^2
' button "data1" text floor(kv)
  a=(kv-drag)/120
' button "data2" text int(10*a)/10
  v+=(a+ao)*dt/2 ! ao=a ! v=max(v,.1)
  s+=(vo+v)*dt/2 ! vo=v

  rmeter("km/hr","r",200,300,150,0,200,3.6*v,0)
  rmeter(" rpm","b",550,300,150,0,60,rev,0)

'  play_note("18:t",a)
  if a<=0 then dt=1 else dt=min(3,1/a)
'  door: if not bp("data") then door
goto loop
end

def app_init
graphics ! graphics clear ! fill color .76,.76,.64
option angle degrees
for i=1 to 5 ! read .gear(i) ! next i
for i=0 to 11 ! read .notes$(i) ! next i
data 8,5.5,3.8,2.6,1.8
data "C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
rmeter("km/hr","r",200,300,150,0,200,0,1)
rmeter(" rpm","b",550,300,150,0,60,0,1)
draw size 10
draw arc 200,300,170,-105,90,1
draw arc 550,300,170,-75,90
d=18
draw arc 375,874,770,-90+d,-90-d,1
'draw line 200,130 to 550,130
draw line 200,470 to 550,470
draw size 3
draw rect 360,306 to 390,450
gearbox(0)
slider "throttle" value 0 at 650,840 size 340 angle -90
draw text "Throttle" at 610,880
draw line 600,540 to 600,840
for y=100 to 0 step -20
  draw line 600,840-3*y to 620,840-3*y
  draw text y at 680,830-3*y
  next y
set buttons custom
set buttons font size 60
button "up" text "+" at 50,520 size 80,80
button "down" text "-" at 50,640 size 80,80
draw text "Gear box" at 40,740
set buttons font size 40
button "start" text "Start" at 315,520 size 120,80
set buttons font size 20
' button "data1" text 0 at 315,640 size 120,30
' button "data2" text 0 at 315,680 size 120,30


end def

def gearbox(g)
draw font size 20 ! draw color .6,.6,.6
for y=309 to 410 step 20
  draw text str$(floor((409-y)/20)) at 367,y
  next y
draw text "R" at 367,429
draw color 0,0,0
draw text g at 367,409-20*g
end def

def play_note(a$,herz)
n$=a$ & freq2note$(herz)
notes set n$ ! notes play
go_on: if notes_time()<.7*notes_length() then go_on
end def

def freq2note$(herz)
if herz<16.35 or herz>7902.13 then return ""
note=int(17.31234*ln(herz)-96.3763)
doct=floor(note/12) ! oct=4+doct ! note-=12*doct
return .notes$(note) & oct
end def

' rmeter function, where:
' tit$ = text, printed on the meter
' col$ = color of meter, "r", "g" , "b" for red, green and blue
' xc and yc = centre of meter on the screen
' rad = size (radius) of the meter
' minval and maxval = lowest and highest value on the scale
' val = the value to be displayed by the meter
' mode=1 : creation of one meter
' mode=0 : use meter to display value
' mode=-1: delete meter from screen
'
def rmeter(tit$,col$,xc,yc,rad,minval,maxval,val,mode)
if mode=-1 then
  fill circle xc,yc size rad+2
  return
  end if
graphics lock
  if mode=0 then mode_0
  set_col("n")
  draw size 3 ! draw circle xc,yc size rad
  draw size 4 ! draw circle xc,yc size rad-6
  set_col(col$)
  draw size 3 ! draw circle xc,yc size rad-3
  set_col("n")
  draw arc xc,yc,rad-50,130,410
  draw size 3
  for a=130 to 410 step 28
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-42)*cos(a) ! ye=yc+(rad-42)*sin(a)
    draw line xs,ys to xe,ye
    next a
  draw size 1
  for a=130 to 410 step 7
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-44)*cos(a) ! ye=yc+(rad-44)*sin(a)
    draw line xs,ys to xe,ye
    next a
  delta=(maxval-minval)/10 ! sval=minval
  draw font size 15
  for a=130 to 410 step 28
    xs=xc+(rad-30)*cos(a)-18 ! ys=yc+(rad-30)*sin(a)-10
    draw text n2a$(sval,3,0) at xs,ys
    sval=sval+delta
    next a
  draw font size 20
mode_0:
  fill circle xc,yc size rad-52
  xs=xc+(rad-80)*cos(112) ! ys=yc+(rad-80)*sin(112)
  set_col(col$)
  fill circle xc,yc size 10 ! draw text tit$ at xs,ys
  set_col("n")
  if val<minval then val=minval
  if val>maxval then val=maxval
  beta=130+280*(val-minval)/(maxval-minval)
  xs=xc-30*cos(beta) ! ys=yc-30*sin(beta)
  xe=xc+(rad-54)*cos(beta) ! ye=yc+(rad-54)*sin(beta)
  draw size 6 ! draw line xs,ys to xe,ye
graphics unlock
end def

def set_col(col$)
if col$="r" or col$="R" then
  draw color 1,0,0 ! fill color 1,0,0
  end if
if col$="g" or col$="G" then
  draw color 0,.6,0 ! fill color 0,.6,0
  end if
if col$="b" or col$="B" then
  draw color 0,0,1 ! fill color 0,0,1
  end if
if col$="n" or col$="N" then
  draw color 0,0,0 ! fill color .76,.76,.64
  end if
enddef

def n2a$(num,lang,dec)
b$="               "
fac=10^dec
num$=floor(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def

def pre_pad$(w,a$)
sp$="               "
tot=w-len(a$)
if tot>0 then pre_pad$=substr$(sp$,1,tot) & a$ else pre_pad$=a$
end def

def bp(a$) = button_pressed(a$)
def sc(a$) = slider_changed(a$)


User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: My Toyota

Post by Mr. Kibernetik »

Nice indicators!

When you shift down, speed is not decreased. As a result you can go 140 km/h on the 1st gear.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: My Toyota

Post by Henko »

Yes i know. It's not a serious app and has low priority.
I'm working right now on a morse 'listener', with wich you can follow morse channels on the short wave radio 'on the fly' in text on the screen, or in spoken text, using the SAY commands in version 5.1

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: My Toyota

Post by Mr. Kibernetik »

Yes, mike input is a necessary thing...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: My Toyota

Post by Operator »

@Henko
Does the rpm-sound work?
I un-commented [..] ' play_note("18:t",a)
but I can't hear anything..

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: My Toyota

Post by Henko »

Operator wrote:@Henko
Does the rpm-sound work?
I un-commented [..] ' play_note("18:t",a)
but I can't hear anything..
No, i didn't finish the sound, it became too clumsy and I lost interest :lol:

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: My Toyota

Post by Operator »

:roll:
Please post it as a snippet (with rpm/pitch-slider), even if "clumsy" :D ...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: My Toyota

Post by Henko »

Operator wrote::roll:
Please post it as a snippet (with rpm/pitch-slider), even if "clumsy" :D ...
There is no more than in the original posting. I tried some stand-alone sound efects but they were not good. I deleted them.

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: My Toyota (now with sound)

Post by Operator »

Now Henko's Toyota has engine SOUND :D Download the soundfont and run the slightly modified code.
For other uses just copy the rev_sound() function..., enjoy...

Soundfont-link:
https://www.dropbox.com/s/gc627bcp87j5w8j/FMOD.sf2?dl=0

Code: Select all

REM My Toyota by Henko
REM sound update by Operator
REM see DEF rev_sound()

DIM NOTES$(12),gear(6)
app_init
t=0 ! dt=.1 ! v=.1 ! s=0 ! alfa=0 ! u=0 !thr=0 ! gr=0 ! n=0
loop1: SLOWDOWN
  IF bp("start") THEN
    thr=0.05
    SLIDER "throttle" VALUE thr
    p=5
    ELSE ! GOTO loop1
    END IF
    
LOP:
  t+=dt
  IF sc("throttle") THEN 
    p=100*SLIDER_VALUE("throttle")
    IF gr=0 THEN ntar=3.7*p-3.6
    END IF
  IF bp("up") AND gr<5 THEN ! gr+=1 ! gearbox(gr) ! END IF
  IF bp("down") AND gr>0 THEN ! gr-=1 ! gearbox(gr) ! END IF

  IF gr=0 THEN
    n=.5*ntar+.5*n
    'min rev. increased
    rev=.6*n+6+RND(1)
    power=0
    db=0.00025*dt
  ELSE
    n=MIN(90,gear(gr)*v)
    rev=.6*n
    power=(p-0.7)*n-0.27*n^2
    db=0.0000011*power*dt
  END IF

  kv=power/v
  IF v>0 THEN fric=.02*COS(alfa) ELSE fric=0
  drag=1200*(SIN(alfa)+fric)+0.044*(v+u)^2
' button "data1" text floor(kv)
  a=(kv-drag)/120
' button "data2" text int(10*a)/10
  v+=(a+ao)*dt/2 ! ao=a ! v=MAX(v,.1)
  s+=(vo+v)*dt/2 ! vo=v

  rmeter("km/hr","r",200,300,150,0,200,3.6*v,0)
  rmeter(" rpm","b",550,300,150,0,60,rev,0)
  
  'rev normalized and limited to max 6400rpm
  rev2 = MIN(rev/64,64)
  rev_sound(rev2)

  'play_note("18:t",a+10)
  IF a<=0 THEN dt=1 ELSE dt=MIN(3,1/a)
  'door: IF NOT bp("data") THEN door
GOTO LOP
END


DEF app_init
GRAPHICS ! GRAPHICS CLEAR ! FILL COLOR .76,.76,.64
OPTION ANGLE DEGREES
FOR i=1 TO 5 ! READ .gear(i) ! NEXT i
FOR i=0 TO 11 ! READ .NOTES$(i) ! NEXT i
DATA 8,5.5,3.8,2.6,1.8
DATA "C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
rmeter("km/hr","r",200,300,150,0,200,0,1)
rmeter(" rpm","b",550,300,150,0,60,0,1)
DRAW SIZE 10
DRAW ARC 200,300,170,-105,90,1
DRAW ARC 550,300,170,-75,90
d=18
DRAW ARC 375,874,770,-90+d,-90-d,1
'draw line 200,130 to 550,130
DRAW LINE 200,470 TO 550,470
DRAW SIZE 3
DRAW RECT 360,306 TO 390,450
gearbox(0)
SLIDER "throttle" VALUE 0 AT 650,840 SIZE 340 ANGLE -90
DRAW TEXT "Throttle" AT 610,880
DRAW LINE 600,540 TO 600,840
FOR y=100 TO 0 STEP -20
  DRAW LINE 600,840-3*y TO 620,840-3*y
  DRAW TEXT y AT 680,830-3*y
  NEXT y
SET BUTTONS CUSTOM
SET BUTTONS FONT SIZE 60
BUTTON "up" TEXT "+" AT 50,520 SIZE 80,80
BUTTON "down" TEXT "-" AT 50,640 SIZE 80,80
DRAW TEXT "Gear box" AT 40,740
SET BUTTONS FONT SIZE 40
BUTTON "start" TEXT "Start" AT 315,520 SIZE 120,80
SET BUTTONS FONT SIZE 20
' button "data1" text 0 at 315,640 size 120,30
' button "data2" text 0 at 315,680 size 120,30
END DEF

DEF gearbox(g)
DRAW FONT SIZE 20 ! DRAW COLOR .6,.6,.6
FOR y=309 TO 410 STEP 20
  DRAW TEXT STR$(FLOOR((409-y)/20)) AT 367,y
  NEXT y
DRAW TEXT "R" AT 367,429
DRAW COLOR 0,0,0
DRAW TEXT g AT 367,409-20*g
END DEF

DEF play_note(a$,herz)
n$=a$ & freq2note$(herz)
NOTES SET n$ ! NOTES PLAY
go_on: IF NOTES_TIME()<.7*NOTES_LENGTH() THEN go_on
END DEF

DEF freq2note$(herz)
IF herz<16.35 OR herz>7902.13 THEN RETURN ""
note=INT(17.31234*LN(herz)-96.3763)
doct=FLOOR(note/12) ! oct=4+doct ! note-=12*doct
RETURN .NOTES$(note) & oct
END DEF

' rmeter function, where:
' tit$ = text, printed on the meter
' col$ = color of meter, "r", "g" , "b" for red, green and blue
' xc and yc = centre of meter on the screen
' rad = size (radius) of the meter
' minval and maxval = lowest and highest value on the scale
' val = the value to be displayed by the meter
' mode=1 : creation of one meter
' mode=0 : use meter to display value
' mode=-1: delete meter from screen
'
DEF rmeter(tit$,col$,xc,yc,rad,minval,maxval,VAL,MODE)
IF MODE=-1 THEN
  FILL CIRCLE xc,yc SIZE rad+2
  RETURN
  END IF
GRAPHICS lock
  IF MODE=0 THEN mode_0
  set_col("n")
  DRAW SIZE 3 ! DRAW CIRCLE xc,yc SIZE rad
  DRAW SIZE 4 ! DRAW CIRCLE xc,yc SIZE rad-6
  set_col(col$)
  DRAW SIZE 3 ! DRAW CIRCLE xc,yc SIZE rad-3
  set_col("n")
  DRAW ARC xc,yc,rad-50,130,410
  DRAW SIZE 3
  FOR a=130 TO 410 STEP 28
    xs=xc+(rad-50)*COS(a) ! ys=yc+(rad-50)*SIN(a)
    xe=xc+(rad-42)*COS(a) ! ye=yc+(rad-42)*SIN(a)
    DRAW LINE xs,ys TO xe,ye
    NEXT a
  DRAW SIZE 1
  FOR a=130 TO 410 STEP 7
    xs=xc+(rad-50)*COS(a) ! ys=yc+(rad-50)*SIN(a)
    xe=xc+(rad-44)*COS(a) ! ye=yc+(rad-44)*SIN(a)
    DRAW LINE xs,ys TO xe,ye
    NEXT a
  delta=(maxval-minval)/10 ! sval=minval
  DRAW FONT SIZE 15
  FOR a=130 TO 410 STEP 28
    xs=xc+(rad-30)*COS(a)-18 ! ys=yc+(rad-30)*SIN(a)-10
    DRAW TEXT n2a$(sval,3,0) AT xs,ys
    sval=sval+delta
    NEXT a
  DRAW FONT SIZE 20
mode_0:
  FILL CIRCLE xc,yc SIZE rad-52
  xs=xc+(rad-80)*COS(112) ! ys=yc+(rad-80)*SIN(112)
  set_col(col$)
  FILL CIRCLE xc,yc SIZE 10 ! DRAW TEXT tit$ AT xs,ys
  set_col("n")
  IF VAL<minval THEN VAL=minval
  IF VAL>maxval THEN VAL=maxval
  beta=130+280*(VAL-minval)/(maxval-minval)
  xs=xc-30*COS(beta) ! ys=yc-30*SIN(beta)
  xe=xc+(rad-54)*COS(beta) ! ye=yc+(rad-54)*SIN(beta)
  DRAW SIZE 6 ! DRAW LINE xs,ys TO xe,ye
GRAPHICS unlock
END DEF

DEF set_col(col$)
IF col$="r" OR col$="R" THEN
  DRAW COLOR 1,0,0 ! FILL COLOR 1,0,0
  END IF
IF col$="g" OR col$="G" THEN
  DRAW COLOR 0,.6,0 ! FILL COLOR 0,.6,0
  END IF
IF col$="b" OR col$="B" THEN
  DRAW COLOR 0,0,1 ! FILL COLOR 0,0,1
  END IF
IF col$="n" OR col$="N" THEN
  DRAW COLOR 0,0,0 ! FILL COLOR .76,.76,.64
  END IF
ENDDEF

DEF n2a$(num,lang,DEC)
b$="               "
fac=10^DEC
num$=FLOOR(fac*num+.5)/fac
tot=lang-LEN(num$)
IF tot<1 THEN tot=1
a$=SUBSTR$(b$,1,tot) & num$
n2a$=a$
END DEF

DEF pre_pad$(w,a$)
sp$="               "
tot=w-LEN(a$)
IF tot>0 THEN pre_pad$=SUBSTR$(sp$,1,tot) & a$ ELSE pre_pad$=a$
END DEF

DEF bp(a$) = BUTTON_PRESSED(a$)
DEF sc(a$) = SLIDER_CHANGED(a$)



DEF rev_sound(rev_n)
'will sound normalized rpm-range (0->1)
'soundfont's midi keyrange: 36 -> 71
'midi note volume fixed to 120
'soundfont made with Fmod samples and 'Polyphone

IF rev_setup = 0 THEN
  'load soundfont
  NOTES INSTRUMENTS "FMOD.sf2"
  'load soundfont instrument 
  NOTES MIDI 0,12,0
  rev_setup = 1
END IF

'map note range 36-71 to nor. rpm range 0-1
note = MIN(36 + rev_n*35,71)

'get floor value of midi_note
note_f_old = note_f
note_f = FLOOR(note)

'calc. pitchbend value
dm_note = note - note_f
pitchB = 64 + INT(dm_note*31)

'pitchbend (pitch changes also as note plays)
NOTES MIDI 0,14,0,pitchB

'play note
IF note_f <> note_f_old THEN
  NOTES MIDI 0,9,note_f,120    'play new note
  NOTES MIDI 0,9,note_f_old,0  'stop old note
END IF
END DEF

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: My Toyota

Post by rbytes »

Nice adaptation. The sound are very realistic. I'm glad I don't have to operate a clutch too!
Those meters sure work well. I wish there was a way to have them respond to sound or mike levels.
The only thing that gets me down is gravity...

Post Reply