|
| 1 | +------------------------------------------------------------------------------ |
| 2 | +-- -- |
| 3 | +-- Copyright (C) 2018, AdaCore -- |
| 4 | +-- -- |
| 5 | +-- Redistribution and use in source and binary forms, with or without -- |
| 6 | +-- modification, are permitted provided that the following conditions are -- |
| 7 | +-- met: -- |
| 8 | +-- 1. Redistributions of source code must retain the above copyright -- |
| 9 | +-- notice, this list of conditions and the following disclaimer. -- |
| 10 | +-- 2. Redistributions in binary form must reproduce the above copyright -- |
| 11 | +-- notice, this list of conditions and the following disclaimer in -- |
| 12 | +-- the documentation and/or other materials provided with the -- |
| 13 | +-- distribution. -- |
| 14 | +-- 3. Neither the name of the copyright holder nor the names of its -- |
| 15 | +-- contributors may be used to endorse or promote products derived -- |
| 16 | +-- from this software without specific prior written permission. -- |
| 17 | +-- -- |
| 18 | +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -- |
| 19 | +-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -- |
| 20 | +-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -- |
| 21 | +-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -- |
| 22 | +-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -- |
| 23 | +-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -- |
| 24 | +-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -- |
| 25 | +-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -- |
| 26 | +-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -- |
| 27 | +-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -- |
| 28 | +-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- |
| 29 | +-- -- |
| 30 | +------------------------------------------------------------------------------ |
| 31 | +with MicroBit.I2C; |
| 32 | +package body MicroBit.MotorDriver is |
| 33 | + |
| 34 | + |
| 35 | + MD : DFR0548.MotorDriver (MicroBit.I2C.ControllerExt); |
| 36 | + |
| 37 | + procedure Initialize is |
| 38 | + begin |
| 39 | + if not MicroBit.I2C.InitializedExt then |
| 40 | + MicroBit.I2C.InitializeExt; |
| 41 | + end if; |
| 42 | + |
| 43 | + MD.Initialize; |
| 44 | + MD.Set_Frequency_Hz (50); --50 Hz |
| 45 | + end Initialize; |
| 46 | + |
| 47 | + procedure Drive (Direction : Directions; |
| 48 | + Speed : Speeds := (4095,4095,4095,4095)) is |
| 49 | + --Note: See implementation of wheel to be (Forward, Backward) |
| 50 | + --!! They can never be a non zero value at the same time !! eg. rf => (4000,2000) is illegal |
| 51 | + --rf = right front wheel, rb = right back wheel, etc. |
| 52 | + --for example direction see: |
| 53 | + begin |
| 54 | + case Direction is |
| 55 | + when Forward => |
| 56 | + Drive_Wheels(rf => (Speed.rf, 0), |
| 57 | + rb => (Speed.rb, 0), |
| 58 | + lf => (Speed.lf, 0), |
| 59 | + lb => (Speed.lb, 0)); |
| 60 | + when Left => |
| 61 | + Drive_Wheels(rf => (Speed.rf ,0), |
| 62 | + rb => (0, Speed.rb), |
| 63 | + lf => (0, Speed.lf), |
| 64 | + lb => (Speed.lb, 0)); |
| 65 | + when Forward_Left => --forward left diagonal |
| 66 | + Drive_Wheels(rf => (Speed.rf, 0), |
| 67 | + rb => (0, 0), |
| 68 | + lf => (0, 0), |
| 69 | + lb => (Speed.lb, 0)); |
| 70 | + when Backward_Left => --backward left diagonal |
| 71 | + Drive_Wheels(rf => (0, Speed.rf), |
| 72 | + rb => (0, 0), |
| 73 | + lf => (0, 0), |
| 74 | + lb => (0, Speed.lb)); |
| 75 | + when Turning => --Same as Forward, wheelspeed left < wheelspeed right results in curved left |
| 76 | + Drive_Wheels(rf => (Speed.rf, 0), |
| 77 | + rb => (Speed.rb, 0), |
| 78 | + lf => (Speed.lf, 0), |
| 79 | + lb => (Speed.lb ,0)); |
| 80 | + when Lateral_Left => |
| 81 | + Drive_Wheels(rf => (Speed.rf,0), |
| 82 | + lb => (0, 0), |
| 83 | + lf => (0,Speed.lf), |
| 84 | + rb => (0,0)); |
| 85 | + when Rotating_Left => |
| 86 | + Drive_Wheels(rf => (Speed.rf,0), |
| 87 | + lb => (Speed.rb, 0), |
| 88 | + lf => (0,Speed.lf), |
| 89 | + rb => (0,Speed.lb)); |
| 90 | + when Stop => |
| 91 | + Drive_Wheels(rf => (0, 0), |
| 92 | + rb => (0, 0), |
| 93 | + lf => (0, 0), |
| 94 | + lb => (0, 0)); |
| 95 | + end case; |
| 96 | + end Drive; |
| 97 | + |
| 98 | + procedure Drive_Wheels(rf : Wheel; |
| 99 | + rb : Wheel; |
| 100 | + lf : Wheel; |
| 101 | + lb : Wheel ) is |
| 102 | + begin |
| 103 | + MD.Set_PWM_Wheels (rf, rb, lf, lb); |
| 104 | + end Drive_Wheels; |
| 105 | + |
| 106 | + procedure Servo (ServoPin : ServoPins ; |
| 107 | + Angle : Degrees) |
| 108 | + is |
| 109 | + begin |
| 110 | + MD.Set_Servo(ServoPin, Angle); |
| 111 | + end Servo; |
| 112 | + |
| 113 | +begin |
| 114 | + Initialize; |
| 115 | +end MicroBit.MotorDriver; |
0 commit comments