User Manual
MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
130
ABS
The Absolute Value (ABS) converts a signed number to its absolute value. The absolute value of
a number is the value that represents its difference from 0. The absolute value of -4 is 4. If the
number is positive the result will always be the same number returned:
temp = abs(-1234)
temp = abs(1234)
In the example above, the result will always be 1234 since the difference of 0 from -1234 is
1234.
SIN, COS
In integer arithmetic, some modications to the way sine and cosine work have been made. For
example, in oating point math, the expression:
ans = sin(angle)
where angle is 1 radian, would return a value of 0.841... for ans. In fact, the sine of an angle
must always be a fractional value between -1 and 1. MBasic can not deal with fractional values
for integer math so SIN and COS are made to work with integers.
Since we are dealing with binary integers, we divide the circle into 256 (rather than 360) parts.
This means that a right angle is expressed as 64 units, rather than 90 degrees. When working
with Basic Micro Studio angular units give you a precision of about 1.4 degrees.
The result of the SIN or COS function is a signed number in the range of –127 to +128. This
number divided by 128 gives the fractional value of SIN or COS.
In most “real world” applications, the angle does not need to be in degrees, nor does the result
need to be in decimal form. The following example shows a possible use of SIN values. If a
sensor returns the angle of a robotic control arm as a number from 0 to 64, where 0 is parallel
and 64 is a right angle. We want to take action based on the sine of the angle.
limit var byte
angle var byte
loop
(code that inputs the value of “angle”)
limit = sin angle
if limit > 24 then rst
if limit > 48 then second
goto loop
rst
code to warn of excessive angle
goto loop
second
code to shut down equipment
etc...