Making a turtle stop and then go after a special event - Robotic lawn mower simulation project

自闭症网瘾萝莉.ら 提交于 2019-12-12 10:29:14

问题


I am trying to simulate a robotic lawn mower on Netlogo. My first goal was it finds its way home to recharge itself when the battery is low.

This is now done (thanks Luke !) but I can't figure how to make the lawnmower stop when it reaches the house (it keeps moving 2 patches around). Therefore my energy slider goes infinitely below zero. I first thought of adding an event "recharge" and placing it after "check death" in to go with a if instance. But :

  • How do I say if car goes on specific patch : stop and get energy ? Should I use a special patch color for the house and then code if car goes on blue patch..... ?
  • Could adding the recharge event at the end of the to go elements would possibly create a conflict because recharge only goes when on low battery ? Would putting it in check death be a solution ?

Then I would like it to go back to work after instant recharge.

Any ideas ?

Here's the code :

breed [cars car]
cars-own [target]

breed [houses house]
to setup
  clear-all
  setup-patches
  setup-cars ;;represent lawn mower
  setup-house
  reset-ticks
end

to setup-patches
  ask patches [set pcolor green] ;; Setup grass patches
  ask patches with [
    pxcor = max-pxcor or
    pxcor = min-pxcor or
    pycor = max-pycor or
    pycor = min-pycor ] [
    set pcolor red ;; Setup the borders of the garden
  ]
end

to setup-cars
create-cars 1 [
    setxy 8 8
    set target one-of houses
  ]

end

to setup-house
  set-default-shape houses "house"
  ask patch 7 8 [sprout-houses 1]
end

to place-walls
  if mouse-down? [
    ask patch mouse-xcor mouse-ycor [ set pcolor red ]
    display
  ]
end


to go
  move-cars
  cut-grass
  check-death ;; Check % battery.
  tick
end

to move-cars
  ask cars
  [
    ifelse [pcolor] of patch-ahead 1 = red
      [ lt random-float 360 ]   ;; see red patch ahead turn left.
      [ fd 1 ]                  ;; otherwise it is ok to go.
    set energy energy - 1
]
  tick
end

to cut-grass
  ask cars [
    if pcolor = green [
      set pcolor gray
    ]
  ]
end

to check-death ;; when low energy lawn mower will go back to house
  ask cars [
    ifelse energy >= 150
    [ set label "Energy ok" ]
    [ set label "Low Energy, returning to base"
      set target min-one-of houses [distance myself]
      face target
      ifelse distance target < 1
      [ move-to target ]
      [ fd 1 ]
    ]
  ]
end

回答1:


You can use a logical flag (eg charging?) in conjunction with a counter (eg charge-time) to do this. Try modifying your cars-own definitions like this:

cars-own [target charging? charge-time]

and setup-cars like this:

to setup-cars
  create-cars 1 [
    setxy 8 8
    set target one-of houses
    set charging? false
  ]  
end

Then you can have the mower do different things based on whether charging? is true or false. Try modifying your move-cars and check-death to accommodate these changes (side note- you've got a tick in both go and move-cars):

to move-cars
  ask cars [ 
    ifelse charging? [
      set charge-time charge-time + 1
      if charge-time > 14 [
        set energy 200
        set charging? false
        set charge-time 0
      ]
    ] [
      ifelse [pcolor] of patch-ahead 1 = red
      [ lt random-float 360 ]   ;; see red patch ahead turn left.
      [ fd 1 ]                  ;; otherwise it is ok to go.
      set energy energy - 1
    ]
  ]
end

to check-death ;; when low energy lawn mower will go back to house
  ask cars [
    ifelse energy >= 150
    [ set label "Energy ok" ]
    [ set label "Low Energy, returning to base"
      set target min-one-of houses [distance myself]
      face target
      ifelse distance target < 1
      [ move-to target 
        set charging? true
      ]
      [ fd 1 ]
    ]
  ]
end

I suggest you change energy a turtle variable like car-energy so that the turtles values are independent of your slider. That would mean you could have multiple mowers and they could reset their energy level to the slider setting once they are charged! You would just include a line like

set car-energy energy

in your setup-cars (and then modify so that energy in your car procedures is instead car-energy).



来源:https://stackoverflow.com/questions/47014217/making-a-turtle-stop-and-then-go-after-a-special-event-robotic-lawn-mower-simu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!