Skip to content

Instantly share code, notes, and snippets.

@shwars
Forked from kashmervil/transporter.fs
Last active August 29, 2015 14:10
Show Gist options
  • Save shwars/6d62959737788ed0ec8c to your computer and use it in GitHub Desktop.
Save shwars/6d62959737788ed0ec8c to your computer and use it in GitHub Desktop.
open System
let L = 14. // Space between wheels || Your values might be quite different
let R = 2.7 // Wheel's radius ||
let ticksPerRotation = 12000. // ~ amount of ticks is taken by one full rotation
let desiredAngle = int ((L/R/4.) * ticksPerRotation) // Math routine to calculate how many ticks is taken to turn
type Direction = Forward | Backward | Left | Right
// <Twitter boilerplate part>
let direction = new Trik.Notifier<_>()
let dir (key: ConsoleKey) =
match key with
| ConsoleKey.UpArrow -> Forward
| ConsoleKey.DownArrow -> Backward
| ConsoleKey.LeftArrow -> Left
| ConsoleKey.RightArrow -> Right
| _ -> Forward
let loop() =
while true do
let curr = dir <| Console.ReadKey().Key
//printfn "%A" curr
direction.OnNext curr
let twitter = direction.Publish
// </Twitter boilerplate part>
let model = new Trik.Model()
let encoderL = model.Encoder.["B4"]
let encoderR = model.Encoder.["B1"]
let encoderRead() = (encoderL.Read(), encoderR.Read())
let first = encoderRead()
let setWheels (l,r) = model.Motor.["M4"].SetPower l; model.Motor.["M1"].SetPower r
twitter.Subscribe(fun x -> printfn "%A" x
match x with
| Forward -> setWheels (100,100)
| Backward -> setWheels (-100,-100)
| Left ->
setWheels (0,100)
encoderR.Reset()
let angle = desiredAngle - 65536
while -encoderR.Read() < angle do
System.Threading.Thread.Sleep 100
setWheels (100,100)
| Right ->
setWheels (100,0)
encoderL.Reset()
while encoderL.Read() < desiredAngle do
System.Threading.Thread.Sleep 100
setWheels (100,100)
) |> ignore
printfn "Ready"
loop() //Press arrow keys to manipulate robot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment