diff options
author | SudhakarKuma | 2021-05-03 22:14:29 +0530 |
---|---|---|
committer | SudhakarKuma | 2021-05-03 22:14:29 +0530 |
commit | 7080068e30db8bac72b209cddf9746c63f6c1535 (patch) | |
tree | 656e9d4092512a17edba23ad7208c48512c59710 | |
parent | 73b3d5e6715aa6a5cc5472402cdf96b6558e404b (diff) | |
download | FLOSS-Arduino-Book-7080068e30db8bac72b209cddf9746c63f6c1535.tar.gz FLOSS-Arduino-Book-7080068e30db8bac72b209cddf9746c63f6c1535.tar.bz2 FLOSS-Arduino-Book-7080068e30db8bac72b209cddf9746c63f6c1535.zip |
Maintain consistency in servo motor codes
25 files changed, 139 insertions, 162 deletions
diff --git a/tools/arduino-firmware/arduino-firmware.ino b/tools/arduino-firmware/arduino-firmware.ino index 5d7e9ca..9f7a8ae 100644 --- a/tools/arduino-firmware/arduino-firmware.ino +++ b/tools/arduino-firmware/arduino-firmware.ino @@ -197,7 +197,7 @@ void loop() { if (val==49 || val==50) { //servo 1 or 2 pin=val-48; //number of the servo if (pin==1) { - servo1.attach(9); + servo1.attach(5); servo1.write(0); // agv=servo1.read(); // Serial.println(agv); diff --git a/tools/openmodelica/windows/Arduino.mo b/tools/openmodelica/windows/Arduino.mo index 4958cb7..54c0628 100644 --- a/tools/openmodelica/windows/Arduino.mo +++ b/tools/openmodelica/windows/Arduino.mo @@ -909,11 +909,11 @@ Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa); else sComm.cmd_servo_attach(1, 1) "To attach the motor to pin 9 of servo1"; sComm.cmd_servo_move(1, 1, 30) "tell servo to rotate by 30 degrees"; - sComm.delay(3000); + sComm.delay(1000); end if; c_ok := sComm.close_serial(1) "To close the connection safely"; end when; -//sComm.cmd_servo_detach(1,1); + sComm.cmd_servo_detach(1, 1); annotation( experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); end servo_init; @@ -965,7 +965,6 @@ Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa); sComm.cmd_servo_move(1, 1, 45) "Move the servo to 45 degree"; sComm.delay(1000) "be there for one second"; sComm.cmd_servo_detach(1, 1) "Detach the motor"; - sComm.delay(1000); end if; c_ok := sComm.close_serial(1) "To close the connection safely"; end when; @@ -979,27 +978,27 @@ Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa); import strm = Modelica.Utilities.Streams; Integer ok(fixed = false); Integer c_ok(fixed = false); - Integer analog_in(fixed = false); + Integer val(fixed = false); algorithm when initial() then ok := sComm.open_serial(1, 2, 115200) "COM port is 2 and baud rate is 115200"; - sComm.delay(2000); if ok <> 0 then strm.print("Check the serial port and try again"); else sComm.cmd_servo_attach(1, 1) "Attach the motor to pin 9"; sComm.delay(2000); - for i in 1:5000 loop - analog_in := sComm.cmd_analog_in(1, 2) "Read potentiometer value"; - sComm.cmd_servo_move(1, 1, analog_in) "Command the servo motor"; - sComm.delay(1000) "sleep for 1000 milliseconds"; + for i in 1:50 loop + val := sComm.cmd_analog_in(1, 2) "Read potentiometer value"; + val := integer(val * 180 / 1023); + sComm.cmd_servo_move(1, 1, val) "Command the servo motor"; + sComm.delay(500) "sleep for 1000 milliseconds"; end for; sComm.cmd_servo_detach(1, 1) "Detach the motor"; end if; - analog_in := sComm.math_floor(analog_in * (180 / 1023)) "Scale Potentiometer value to 0-180"; c_ok := sComm.close_serial(1) "To close the connection safely"; end when; -//5,000 itterations +// strm.print(String(integer(analog_in * 180 / 1023))); +// analog_in := sComm.math_floor(analog_in * (180 / 1023)) "Scale Potentiometer value to 0-180"; //strm.print(String(analog_in)); annotation( experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); diff --git a/user-code/servo/OpenModelica/servo-init.mo b/user-code/servo/OpenModelica/servo-init.mo index b9e4367..2a50689 100644 --- a/user-code/servo/OpenModelica/servo-init.mo +++ b/user-code/servo/OpenModelica/servo-init.mo @@ -6,17 +6,17 @@ model servo_init "Rotate Servo Motor " Integer c_ok(fixed = false); algorithm when initial() then - ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200"; + ok := sComm.open_serial(1, 2, 115200) "COM port is 2 and baud rate is 115200"; if ok <> 0 then strm.print("Check the serial port and try again"); else - sComm.delay(2000); sComm.cmd_servo_attach(1, 1) "To attach the motor to pin 9 of servo1"; sComm.cmd_servo_move(1, 1, 30) "tell servo to rotate by 30 degrees"; - sComm.delay(3000); + sComm.delay(1000); end if; c_ok := sComm.close_serial(1) "To close the connection safely"; end when; - //sComm.cmd_servo_detach(1,1); - annotation(experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); -end servo_init;
\ No newline at end of file + sComm.cmd_servo_detach(1, 1); + annotation( + experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); +end servo_init; diff --git a/user-code/servo/OpenModelica/servo-loop.mo b/user-code/servo/OpenModelica/servo-loop.mo index c3b083b..4e02780 100644 --- a/user-code/servo/OpenModelica/servo-loop.mo +++ b/user-code/servo/OpenModelica/servo-loop.mo @@ -7,11 +7,10 @@ model servo_loop "Rotate servo motor by 20 degrees 10 times" Integer angle(fixed = true); algorithm when initial() then - ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200"; + ok := sComm.open_serial(1, 2, 115200) "COM port is 2 and baud rate is 115200"; if ok <> 0 then strm.print("Check the serial port and try again"); else - sComm.delay(2000); sComm.cmd_servo_attach(1, 1) "Attach motor to pin 9. 1 means pin 9."; sComm.delay(2000); angle := 20 "Angle by which it has to move"; @@ -23,5 +22,6 @@ algorithm end if; c_ok := sComm.close_serial(1) "To close the connection safely"; end when; - annotation(experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); -end servo_loop;
\ No newline at end of file + annotation( + experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5)); +end servo_loop; diff --git a/user-code/servo/OpenModelica/servo-reverse.mo b/user-code/servo/OpenModelica/servo-reverse.mo index 4374860..23a58d4 100644 --- a/user-code/servo/OpenModelica/servo-reverse.mo +++ b/user-code/servo/OpenModelica/servo-reverse.mo @@ -6,20 +6,20 @@ model servo_reverse Integer c_ok(fixed = false);
algorithm
when initial() then
- ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
+ ok := sComm.open_serial(1, 2, 115200) "COM port is 2 and baud rate is 115200";
+ sComm.delay(2000);
if ok <> 0 then
strm.print("Check the serial port and try again");
else
- sComm.delay(2000);
sComm.cmd_servo_attach(1, 1) "Attach the motor to pin 9. 1 means 9";
sComm.cmd_servo_move(1, 1, 90) "Move the servo to 90 degree";
sComm.delay(1000) "be there for one second";
sComm.cmd_servo_move(1, 1, 45) "Move the servo to 45 degree";
sComm.delay(1000) "be there for one second";
sComm.cmd_servo_detach(1, 1) "Detach the motor";
- sComm.delay(1000);
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
- annotation(experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
-end servo_reverse;
\ No newline at end of file + annotation(
+ experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
+end servo_reverse;
diff --git a/user-code/servo/OpenModelica/servo_pot.mo b/user-code/servo/OpenModelica/servo_pot.mo index 7915e09..5245ee2 100644 --- a/user-code/servo/OpenModelica/servo_pot.mo +++ b/user-code/servo/OpenModelica/servo_pot.mo @@ -4,28 +4,28 @@ model servo_pot "Control Servo Motor using Potentiometer" import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
- Integer analog_in(fixed = false);
+ Integer val(fixed = false);
algorithm
when initial() then
- ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
+ ok := sComm.open_serial(1, 2, 115200) "COM port is 2 and baud rate is 115200";
if ok <> 0 then
strm.print("Check the serial port and try again");
else
- sComm.delay(2000);
sComm.cmd_servo_attach(1, 1) "Attach the motor to pin 9";
sComm.delay(2000);
- for i in 1:5000 loop
- analog_in := sComm.cmd_analog_in(1, 2) "Read potentiometer value";
- analog_in := sComm.math_floor(analog_in * (180 / 1023)) "Scale Potentiometer value to 0-180";
- sComm.cmd_servo_move(1, 1, analog_in) "Command the servo motor";
- sComm.delay(1000) "sleep for 500 milliseconds";
- strm.print("Potentiometer Reading at time" + String(time) + " : " + String(analog_in));
+ for i in 1:50 loop
+ val := sComm.cmd_analog_in(1, 2) "Read potentiometer value";
+ val := integer(val * 180 / 1023);
+ sComm.cmd_servo_move(1, 1, val) "Command the servo motor";
+ sComm.delay(500) "sleep for 1000 milliseconds";
end for;
+ sComm.cmd_servo_detach(1, 1) "Detach the motor";
end if;
- sComm.cmd_servo_detach(1, 1) "Detach the motor";
- sComm.delay(5000);
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
- //5,000 itterations
- annotation(experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
-end servo_pot;
\ No newline at end of file +// strm.print(String(integer(analog_in * 180 / 1023)));
+// analog_in := sComm.math_floor(analog_in * (180 / 1023)) "Scale Potentiometer value to 0-180";
+//strm.print(String(analog_in));
+ annotation(
+ experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
+end servo_pot;
diff --git a/user-code/servo/arduino/servo-init/servo-init.ino b/user-code/servo/arduino/servo-init/servo-init.ino index ec7f363..77d2aa7 100644 --- a/user-code/servo/arduino/servo-init/servo-init.ino +++ b/user-code/servo/arduino/servo-init/servo-init.ino @@ -2,11 +2,10 @@ Servo myservo; // create servo object to control a servo void setup() { Serial.begin(115200); - myservo.attach(9); // attach the servo object on to pin 9 + myservo.attach(5); // attach the servo object on to pin 5 myservo.write(30); // tell servo to rotate by 30 degrees delay(1000); myservo.detach(); } void loop() { } - diff --git a/user-code/servo/arduino/servo-loop/servo-loop.ino b/user-code/servo/arduino/servo-loop/servo-loop.ino index f3dc634..4eba5d6 100644 --- a/user-code/servo/arduino/servo-loop/servo-loop.ino +++ b/user-code/servo/arduino/servo-loop/servo-loop.ino @@ -1,11 +1,11 @@ #include <Servo.h> Servo myservo; // create servo object to control a servo -int angle=20; -int i=0; +int angle = 20; +int i = 0; void setup() { - for(i=1;i<10;i++) { + for(i = 1; i < 10; i++) { Serial.begin(115200); - myservo.attach(9); // attach the servo object on to pin 9 + myservo.attach(5); // attach the servo object on to pin 9 myservo.write(angle*i); // tell servo to rotate by 20 degrees delay(1000); // waits for a sec } @@ -13,4 +13,3 @@ void setup() { } void loop() { } - diff --git a/user-code/servo/arduino/servo-pot/servo-pot.ino b/user-code/servo/arduino/servo-pot/servo-pot.ino index cdcc8df..26bed01 100644 --- a/user-code/servo/arduino/servo-pot/servo-pot.ino +++ b/user-code/servo/arduino/servo-pot/servo-pot.ino @@ -5,8 +5,8 @@ int val; // variable to read the value from the analog pin int i; void setup(){ Serial.begin(115200); - myservo.attach(9); // attach the servo object on to pin 9 - for(i=0;i<5000;++i){ + myservo.attach(5); // attach the servo object on to pin 5 + for(i = 0; i < 50; ++i){ val = analogRead(potpin); // reads a value in (0,1023) through pot val = map(val, 0, 1023, 0, 180); // maps it in the range (0,180) degrees myservo.write(val); // moves the motor to the mapped degree @@ -16,4 +16,3 @@ void setup(){ } void loop(){ } - diff --git a/user-code/servo/arduino/servo-reverse/servo-reverse.ino b/user-code/servo/arduino/servo-reverse/servo-reverse.ino index c994df9..438813a 100644 --- a/user-code/servo/arduino/servo-reverse/servo-reverse.ino +++ b/user-code/servo/arduino/servo-reverse/servo-reverse.ino @@ -2,7 +2,7 @@ Servo myservo; // create servo object to control a servo void setup() { Serial.begin(115200); - myservo.attach(9); // attach the servo object on to pin 9 + myservo.attach(5); // attach the servo object on to pin 5 myservo.write(90); // tell servo to rotate by 90 degrees delay(1000); myservo.write(45); @@ -11,4 +11,3 @@ void setup() { } void loop() { } - diff --git a/user-code/servo/julia/servo-init.jl b/user-code/servo/julia/servo-init.jl index 60fae1c..7306f12 100644 --- a/user-code/servo/julia/servo-init.jl +++ b/user-code/servo/julia/servo-init.jl @@ -2,7 +2,7 @@ using SerialPorts include("ArduinoTools.jl") ser = ArduinoTools.connectBoard(115200) -ArduinoTools.ServoAttach(ser,1) -ArduinoTools.ServoMove(ser,1,30) +ArduinoTools.ServoAttach(ser, 1) +ArduinoTools.ServoMove(ser, 1, 30) sleep(1) close(ser) diff --git a/user-code/servo/julia/servo-loop.jl b/user-code/servo/julia/servo-loop.jl index 2bacfa7..c0dc9ea 100644 --- a/user-code/servo/julia/servo-loop.jl +++ b/user-code/servo/julia/servo-loop.jl @@ -2,12 +2,11 @@ using SerialPorts include("ArduinoTools.jl") ser = ArduinoTools.connectBoard(115200) -ArduinoTools.ServoAttach(ser,1) -sleep(1) +ArduinoTools.ServoAttach(ser, 1) angle = 20 -for i = 1:9 - ArduinoTools.ServoMove(ser,1,angle*i) +for i = 1:10 + ArduinoTools.ServoMove(ser, 1, angle*i) sleep(1) end -ArduinoTools.ServoDetach(ser,1) +ArduinoTools.ServoDetach(ser, 1) close(ser) diff --git a/user-code/servo/julia/servo-pot.jl b/user-code/servo/julia/servo-pot.jl index 523b03a..8c56ad2 100644 --- a/user-code/servo/julia/servo-pot.jl +++ b/user-code/servo/julia/servo-pot.jl @@ -2,13 +2,13 @@ using SerialPorts include("ArduinoTools.jl") ser = ArduinoTools.connectBoard(115200) -ArduinoTools.ServoAttach(ser,1) -for i = 1:500 - p = ArduinoTools.analogRead(ser,2) - p = p*(180/1023) - q = round(Int,floor(p)) - ArduinoTools.ServoMove(ser,1,q) +ArduinoTools.ServoAttach(ser, 1) +for i = 1:50 + val = ArduinoTools.analogRead(ser, 2) + val = val*(180/1023) + val = round(Int, floor(val)) + ArduinoTools.ServoMove(ser, 1, val) sleep(0.5) end -ArduinoTools.ServoDetach(ser,1) +ArduinoTools.ServoDetach(ser, 1) close(ser) diff --git a/user-code/servo/julia/servo-reverse.jl b/user-code/servo/julia/servo-reverse.jl index 36d9ab6..5d62c47 100644 --- a/user-code/servo/julia/servo-reverse.jl +++ b/user-code/servo/julia/servo-reverse.jl @@ -2,9 +2,9 @@ using SerialPorts include("ArduinoTools.jl") ser = ArduinoTools.connectBoard(115200) -ArduinoTools.ServoAttach(ser,1) -ArduinoTools.ServoMove(ser,1,90) +ArduinoTools.ServoAttach(ser, 1) +ArduinoTools.ServoMove(ser, 1, 90) sleep(1) -ArduinoTools.ServoMove(ser,1,45) +ArduinoTools.ServoMove(ser, 1, 45) sleep(1) close(ser) diff --git a/user-code/servo/python/servo-init.py b/user-code/servo/python/servo-init.py index e9fc8ab..d5754bf 100644 --- a/user-code/servo/python/servo-init.py +++ b/user-code/servo/python/servo-init.py @@ -1,9 +1,7 @@ - import os import sys -cwd=os.getcwd() -(setpath,Examples)=os.path.split(cwd) -#print setpath +cwd = os.getcwd() +(setpath, Examples) = os.path.split(cwd) sys.path.append(setpath) from Arduino.Arduino import Arduino @@ -11,32 +9,30 @@ from time import sleep class SERVO_INIT: def __init__(self,baudrate): - self.baudrate=baudrate + self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): - self.obj_arduino=Arduino() - self.port=self.obj_arduino.locateport() - self.obj_arduino.open_serial(1,self.port,self.baudrate) + self.obj_arduino = Arduino() + self.port = self.obj_arduino.locateport() + self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): - self.pin1=9 - self.obj_arduino.cmd_servo_attach(1,1) - self.obj_arduino.cmd_servo_move(1,1,30) + self.pin1 = 5 + self.obj_arduino.cmd_servo_attach(1, 1) + self.obj_arduino.cmd_servo_move(1, 1, 30) sleep(1) - self.obj_arduino.cmd_servo_detach(1,1) + self.obj_arduino.cmd_servo_detach(1, 1) sleep(1) - - def exit(self): self.obj_arduino.close_serial() def main(): - obj_servo=SERVO_INIT(115200) + obj_servo = SERVO_INIT(115200) -if __name__=='__main__': +if __name__== '__main__': main() diff --git a/user-code/servo/python/servo-loop.py b/user-code/servo/python/servo-loop.py index 73bd5c7..43ca310 100644 --- a/user-code/servo/python/servo-loop.py +++ b/user-code/servo/python/servo-loop.py @@ -1,9 +1,7 @@ - import os import sys -cwd=os.getcwd() -(setpath,Examples)=os.path.split(cwd) -#print setpath +cwd = os.getcwd() +(setpath, Examples)=os.path.split(cwd) sys.path.append(setpath) from Arduino.Arduino import Arduino @@ -11,25 +9,25 @@ from time import sleep class SERVO_INCR: def __init__(self,baudrate): - self.baudrate=baudrate + self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): - self.obj_arduino=Arduino() - self.port=self.obj_arduino.locateport() - self.obj_arduino.open_serial(1,self.port,self.baudrate) + self.obj_arduino = Arduino() + self.port = self.obj_arduino.locateport() + self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): - self.pin1=9 - self.obj_arduino.cmd_servo_attach(1,1) + self.pin1 = 5 + self.obj_arduino.cmd_servo_attach(1, 1) sleep(1) - self.angle=20 + self.angle = 20 for i in range(10): - self.obj_arduino.cmd_servo_move(1,1,self.angle*i) - sleep(2) - self.obj_arduino.cmd_servo_detach(1,1) + self.obj_arduino.cmd_servo_move(1, 1, self.angle*i) + sleep(1) + self.obj_arduino.cmd_servo_detach(1, 1) def exit(self): self.obj_arduino.close_serial() diff --git a/user-code/servo/python/servo-pot.py b/user-code/servo/python/servo-pot.py index 9a66f2b..87d3e06 100644 --- a/user-code/servo/python/servo-pot.py +++ b/user-code/servo/python/servo-pot.py @@ -1,9 +1,7 @@ - import os import sys -cwd=os.getcwd() -(setpath,Examples)=os.path.split(cwd) -#print setpath +cwd = os.getcwd() +(setpath, Examples) = os.path.split(cwd) sys.path.append(setpath) from Arduino.Arduino import Arduino @@ -11,37 +9,32 @@ from time import sleep class SERVO_POT: def __init__(self,baudrate): - self.baudrate=baudrate + self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): - self.obj_arduino=Arduino() - self.port=self.obj_arduino.locateport() - self.obj_arduino.open_serial(1,self.port,self.baudrate) + self.obj_arduino = Arduino() + self.port = self.obj_arduino.locateport() + self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): - self.pin1=9 - self.pot=2 - self.pos=0 - self.obj_arduino.cmd_servo_attach(1,1) - sleep(1) - self.angle=18 - for i in range(100): - val=self.obj_arduino.cmd_analog_in(1,self.pot) - print val + self.pin1 = 5 + self.pot = 2 + self.obj_arduino.cmd_servo_attach(1, 1) + for i in range(50): + val = self.obj_arduino.cmd_analog_in(1, self.pot) val = int(val*180/1023) - print (val) - self.obj_arduino.cmd_servo_move(1,1,val) + self.obj_arduino.cmd_servo_move(1, 1 ,val) sleep(0.5) - self.obj_arduino.cmd_servo_detach(1,1) + self.obj_arduino.cmd_servo_detach(1, 1) def exit(self): self.obj_arduino.close_serial() def main(): - obj_servo=SERVO_POT(115200) + obj_servo = SERVO_POT(115200) if __name__=='__main__': main() diff --git a/user-code/servo/python/servo-reverse.py b/user-code/servo/python/servo-reverse.py index d205771..9075773 100644 --- a/user-code/servo/python/servo-reverse.py +++ b/user-code/servo/python/servo-reverse.py @@ -1,9 +1,7 @@ - import os import sys -cwd=os.getcwd() -(setpath,Examples)=os.path.split(cwd) -#print setpath +cwd = os.getcwd() +(setpath, Examples) = os.path.split(cwd) sys.path.append(setpath) from Arduino.Arduino import Arduino @@ -11,34 +9,31 @@ from time import sleep class SERVO_ANGULAR_ROTATION: def __init__(self,baudrate): - self.baudrate=baudrate + self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): - self.obj_arduino=Arduino() - self.port=self.obj_arduino.locateport() - self.obj_arduino.open_serial(1,self.port,self.baudrate) + self.obj_arduino = Arduino() + self.port = self.obj_arduino.locateport() + self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): - self.pin1=9 - self.obj_arduino.cmd_servo_attach(1,1) - self.obj_arduino.cmd_servo_move(1,1,90) - sleep(1) - self.obj_arduino.cmd_servo_move(1,1,45) + self.pin1 = 5 + self.obj_arduino.cmd_servo_attach(1, 1) + self.obj_arduino.cmd_servo_move(1, 1, 90) sleep(1) - self.obj_arduino.cmd_servo_detach(1,1) + self.obj_arduino.cmd_servo_move(1, 1, 45) sleep(1) - - + self.obj_arduino.cmd_servo_detach(1, 1) def exit(self): self.obj_arduino.close_serial() def main(): - obj_servo=SERVO_ANGULAR_ROTATION(115200) + obj_servo = SERVO_ANGULAR_ROTATION(115200) -if __name__=='__main__': +if __name__== '__main__': main() diff --git a/user-code/servo/scilab/servo-init.sce b/user-code/servo/scilab/servo-init.sce index ac3e331..02c8fdf 100644 --- a/user-code/servo/scilab/servo-init.sce +++ b/user-code/servo/scilab/servo-init.sce @@ -1,6 +1,7 @@ -ok = open_serial(1,2,115200) // At port 2 with baud rate of 115200 +ok = open_serial(1, 2, 115200) // At port 2 with baud rate of 115200 if ok ~= 0 error('Check the serial port and try again'); end -cmd_servo_attach(1,1) // To attach the motor to pin 9 -cmd_servo_move(1,1,30) // tell servo to rotate by 30 degrees +cmd_servo_attach(1, 1) // To attach the motor to pin 5 +cmd_servo_move(1, 1, 30) // tell servo to rotate by 30 degrees sleep(1000) +cmd_servo_detach(1, 1) // Detach the motor close_serial(1) diff --git a/user-code/servo/scilab/servo-init.zcos b/user-code/servo/scilab/servo-init.zcos Binary files differindex 291d5d5..ab34b49 100644 --- a/user-code/servo/scilab/servo-init.zcos +++ b/user-code/servo/scilab/servo-init.zcos diff --git a/user-code/servo/scilab/servo-loop.sce b/user-code/servo/scilab/servo-loop.sce index 173e766..87add3a 100644 --- a/user-code/servo/scilab/servo-loop.sce +++ b/user-code/servo/scilab/servo-loop.sce @@ -1,11 +1,10 @@ -ok= open_serial(1,2,115200); // At port 2 with baudrate of 115200 +ok = open_serial(1, 2, 115200); // At port 2 with baudrate of 115200 if ok ~= 0 error('Check the serial port and try again'); end -cmd_servo_attach(1,1) // Attach motor to pin 9. 1 means pin 9. -sleep(1000) -angle=20; // Angle by which it has to move -for i=0:10 - cmd_servo_move(1,1,angle*i) // tell servo to rotate by 20 degrees +angle = 20; // Angle by which it has to move +for i = 0:10 + cmd_servo_attach(1, 1) // Attach motor to pin 5. 1 means pin 9. + cmd_servo_move(1, 1, angle*i) // tell servo to rotate by 20 degrees sleep(1000) // waits for a sec end -cmd_servo_detach(1,1) // Detach the motor +cmd_servo_detach(1, 1) // Detach the motor close_serial(1); //To close the connection safely diff --git a/user-code/servo/scilab/servo-pot.sce b/user-code/servo/scilab/servo-pot.sce index 2d4a7af..721bc25 100644 --- a/user-code/servo/scilab/servo-pot.sce +++ b/user-code/servo/scilab/servo-pot.sce @@ -1,11 +1,11 @@ -ok = open_serial(1,2,115200) // At port 2 with baud rate of 115200 +ok = open_serial(1, 2, 115200) // At port 2 with baud rate of 115200 if ok ~= 0 error('Check the serial port and try again'); end -cmd_servo_attach(1,1) // Attach the motor to pin 9 -for i=1:5000 // 5,000 itterations - p=cmd_analog_in(1,2) // Read potntiometer value - p=floor(p*(180/1023)) // Scale Potentiometer value to 0-180 - cmd_servo_move(1,1,p) // Command the servo motor +cmd_servo_attach(1, 1) // Attach the motor to pin 5 +for i=1:50 // 5,000 itterations + val = cmd_analog_in(1, 2) // Read potntiometer value + val = floor(val*(180/1023)) // Scale Potentiometer value to 0-180 + cmd_servo_move(1, 1, val) // Command the servo motor sleep(500) // sleep for 500 milliseconds end -cmd_servo_detach(1,1)// Detach the motor +cmd_servo_detach(1, 1)// Detach the motor close_serial(1) diff --git a/user-code/servo/scilab/servo-pot.zcos b/user-code/servo/scilab/servo-pot.zcos Binary files differindex ce85c8f..1d1009f 100644 --- a/user-code/servo/scilab/servo-pot.zcos +++ b/user-code/servo/scilab/servo-pot.zcos diff --git a/user-code/servo/scilab/servo-reverse.sce b/user-code/servo/scilab/servo-reverse.sce index 0405d0d..7fe1008 100644 --- a/user-code/servo/scilab/servo-reverse.sce +++ b/user-code/servo/scilab/servo-reverse.sce @@ -1,8 +1,9 @@ -ok = open_serial(1,2,115200) // Connect to Arduino at port 2
+ok = open_serial(1, 2, 115200) // Connect to Arduino at port 2
if ok ~= 0 error('Check the serial port and try again'); end
-cmd_servo_attach(1,1) // Attach the motor to pin 9. 1 means 9
-cmd_servo_move(1,1,90) // Move the servo to 90 degree
-sleep(1000) // be there for one second
-cmd_servo_move(1,1,45) // Move the servo to 45 degree
-sleep(1000) // be there for one second
+cmd_servo_attach(1, 1) // Attach the motor to pin 5. 1 means 9
+cmd_servo_move(1, 1, 90) // Move the servo to 90 degree
+sleep(1000) // be there for one second
+cmd_servo_move(1, 1, 45) // Move the servo to 45 degree
+sleep(1000) // be there for one second
+cmd_servo_detach(1, 1) // Detach the motor
close_serial(1) // To close the connection safely
diff --git a/user-code/servo/scilab/servo-reverse.zcos b/user-code/servo/scilab/servo-reverse.zcos Binary files differindex 1734bc4..68db9b8 100644 --- a/user-code/servo/scilab/servo-reverse.zcos +++ b/user-code/servo/scilab/servo-reverse.zcos |