Page 1 of 1

Arduino’s map() Function

Posted: Fri Apr 10, 2020 6:46 pm
by StarBootics
Hello everyone,

While I'm working on Steering behaviors "Arrival" I have found a video explaining it (See https://www.youtube.com/watch?v=2CL1maXeQCI). It use a Map() function to calculate the speed reduction in relation to the distance to the target point. After some research I have found an implementation that I have converted into a Macro for PB.

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : MappingValues
; File Name : MappingValues.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 10-04-2020
; Last Update : 10-04-2020
; PureBasic code : V5.72
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming notes
;
; Based on a code found here :
;
; https://www.jetmore.org/john/blog/2011/09/arduinos-map-function-and-numeric-distribution/
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Macro MappingValues(x, in_min, in_max, out_min, out_max)

  ((x - (in_min)) * ((out_max) - (out_min)) / ((in_max) - (in_min)) + (out_min))
  
EndMacro 

CompilerIf #PB_Compiler_IsMainFile
  
  For Index = 0 To 100
    Debug StrF(MappingValues(Index, 0.0, 100.0, 0.0, 15.0), 4)
  Next
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics