Skip to content

22bsm056/Digital-Twin-using-python

Repository files navigation

Digital-Twin-using-python

Servo Motor Digital Twin: Technical Report Executive Summary This technical report analyzes a Python implementation of a servo motor digital twin system. The digital twin creates a virtual 3D representation of a physical servo motor, enabling real-time visualization and control. The system operates in two modes:

  1. Hardware-connected mode: Bidirectional communication with a physical Arduino-controlled servo
  2. Simulation mode: Standalone operation without hardware connection The implementation leverages PyGame and OpenGL for 3D visualization, multi-threading for asynchronous hardware communication, and provides intuitive user controls through keyboard and mouse input.
  3. System Architecture The system follows a model-view-controller architecture: 1.1 Core Components • Model: Maintains servo state (current angle, target angle) • View: Renders 3D visualization using OpenGL • Controller: Processes inputs and manages Arduino communication 1.2 Class Structure The primary class ServoDigitalTwin encapsulates all functionality: • Initialization and configuration • 3D rendering and visualization • Serial communication management • User input processing • Animation control 1.3 Utility Functions • detect_arduino_port() : Automatically identifies available Arduino ports • list_ports() : Enumerates all available serial ports • main() : Entry point with command-line argument parsing
  4. Technical Functionality 2.1 Serial Communication System The system establishes bidirectional communication with an Arduino microcontroller: 2.1.1 Connection Management 2.1.2 Communication Protocol • To Arduino: S:[angle]\n (e.g., S:90\n) • From Arduino: A:[angle] (e.g., A:90) 2.1.3 Reliability Features • Robust error recovery with reconnection logic • Timeout handling for unresponsive hardware • Threaded reading to prevent UI blocking 2.2 3D Visualization Engine The visualization system uses PyGame and OpenGL to create a realistic 3D representation: 2.2.1 Servo Components • Base (rendered as a cylinder) • Body (rendered as a cube) • Rotating shaft (cylinder with dynamic rotation) • Horn (cross-shaped servo attachment) 2.2.2 Rendering Techniques python defdefconnect_to_arduino connect_to_arduino( (self self) ): : trytry: : self self. .arduino arduino = = serial serial. .Serial Serial( ( port port= =self self. .port port, , baudrate baudrate= =self self. .baud_rate baud_rate, , timeout timeout= =1 1, , write_timeout write_timeout= =1 1, , bytesize bytesize= =serial serial. .EIGHTBITS EIGHTBITS, , parity parity= =serial serial. .PARITY_NONE PARITY_NONE, , stopbits stopbits= =serial serial. .STOPBITS_ONE STOPBITS_ONE ) )

Connection handling and verification # Connection handling and verification

except except Exception Exception asas e e: : print print( (f"Failed to connect to Arduino: f"Failed to connect to Arduino: { {e e} }" ") )

Fallback to simulation mode # Fallback to simulation mode

• Perspective projection with gluPerspective • Diffuse and ambient lighting • Material properties for realistic appearance • Custom primitive drawing functions 2.2.3 Text Rendering • Status information (current angle, connection status) • PyGame-based font rendering integrated with OpenGL 2.3 User Interaction System Multiple input methods support intuitive control: 2.3.1 Input Methods • Keyboard: Arrow keys adjust servo angle • Mouse: Scroll wheel controls rotation • Command Line: Arguments for initial configuration 2.3.2 Input Processing 2.4 Error Handling & Robustness Comprehensive error management ensures system stability: 2.4.1 Serial Communication • Exception handling for connection failures • Reconnection logic for interrupted connections • Fallback to simulation mode when hardware is unavailable 2.4.2 Rendering Pipeline • Exception catching in rendering functions • Graceful recovery from OpenGL errors • Alternative text rendering methods 2.4.3 Resource Management python defdefhandle_events handle_events( (self self) ): : forfor event event inin pygame pygame. .event event. .getget( () ): : ifif event event. .type type==== pygame pygame. .QUIT QUIT: : self self. .running running = =False False return returnFalse False

Mouse wheel for rotation # Mouse wheel for rotation

elif elif event event. .type type==== pygame pygame. .MOUSEWHEEL MOUSEWHEEL: : delta delta = = event event. .y y * * self self. .step_size step_size new_angle new_angle = =maxmax( (0 0, ,minmin( (180180, , self self. .target_angle target_angle + + delta delta) )) ) self self. .update_angle update_angle( (new_angle new_angle) )

Arrow keys for rotation # Arrow keys for rotation

elif elif event event. .type type==== pygame pygame. .KEYDOWN KEYDOWN: : ifif event event. .key key ==== pygame pygame. .K_LEFT K_LEFT oror event event. .key key ==== pygame pygame. .K_DOWN K_DOWN: : new_angle new_angle = =maxmax( (0 0, , self self. .target_angle target_angle -- self self. .step_size step_size) ) self self. .update_angle update_angle( (new_angle new_angle) ) elif elif event event. .key key ==== pygame pygame. .K_RIGHT K_RIGHT oror event event. .key key ==== pygame pygame. .K_UP K_UP: : new_angle new_angle = =minmin( (180180, , self self. .target_angle target_angle + + self self. .step_size step_size) ) self self. .update_angle update_angle( (new_angle new_angle) ) elif elif event event. .key key ==== pygame pygame. .K_ESCAPE K_ESCAPE: : self self. .running running = =False False return returnFalse False return returnTrue True • Proper cleanup of OpenGL resources • Serial port closure on exit • Thread termination during shutdown 3. Implementation Details 3.1 Port Detection System The system includes intelligent cross-platform port detection: python def detect_arduino_port():

Try to find Arduino via serial.tools.list_ports

try: import serial.tools.list_ports ports = list(serial.tools.list_ports.comports())

First look for Arduino or USB Serial Device

for port in ports: if "Arduino" in port.description or "USB" in port.description: return port.device

If available, return first port

if ports: return ports[0].device except: pass

OS-specific fallbacks

if sys.platform.startswith('win'): return 'COM3' elif sys.platform.startswith('linux'): for device in ['/dev/ttyACM0', '/dev/ttyUSB0', '/dev/ttyS0']: if os.path.exists(device): return device elif sys.platform.startswith('darwin'): return '/dev/cu.usbmodem1101' return None # Simulation mode 3.2 Animation System For realistic motion, the servo uses smooth transitions: 3.3 3D Model Construction The servo model combines primitive shapes: 3.3.1 Cylinder Generation 3.3.2 Horn Geometry python defdefsmooth_angle_update smooth_angle_update( (self self) ): : ififabsabs( (self self. .angle angle -- self self. .target_angle target_angle) )> >0.50.5: : direction direction = =1 1ifif self self. .target_angle target_angle > > self self. .angle angle else else--1 1 self self. .angle angle +=+= direction direction * *1.51.5 # Animation speed # Animation speed

Prevent overshooting # Prevent overshooting

ifif direction direction > >0 0andand self self. .angle angle > > self self. .target_angle target_angle: : self self. .angle angle = = self self. .target_angle target_angle elif elif direction direction < <0 0andand self self. .angle angle < < self self. .target_angle target_angle: : self self. .angle angle = = self self. .target_angle target_angle python defdefdraw_cylinder draw_cylinder( (self self, , base_radius base_radius, , top_radius top_radius, , height height, , slices slices) ): :

Draw side surface # Draw side surface

glBegin    glBegin( (GL_QUAD_STRIP GL_QUAD_STRIP) )

forfor i i ininrange range( (slices slices + +1 1) ): : angle angle = =2.02.0* * math math. .pi pi * * i i / / slices slices x x, , y y = = math math. .coscos( (angle angle) ), , math math. .sinsin( (angle angle) )

Calculate normals # Calculate normals

    nx        nx, , ny  ny = = x x, , y y
    norm         norm = = math math. .sqrt sqrt( (nxnx* *nx nx + + ny ny* *nyny) )

ifif norm norm > >0 0: : nx nx, , ny ny = = nx nx/ /norm norm, , ny ny/ /norm norm

    glNormal3f        glNormal3f( (nxnx, , ny ny, ,0 0) )
    glVertex3f        glVertex3f( (base_radius base_radius * * x x, , base_radius  base_radius * * y y, ,0 0) )
    glVertex3f        glVertex3f( (top_radius top_radius * * x x, , top_radius  top_radius * * y y, , height height) )
glEnd    glEnd( () )

Draw top and bottom caps # Draw top and bottom caps

self    self. .draw_circle draw_circle( (0 0, ,0 0, ,0 0, , base_radius base_radius, , slices slices, ,False False) )
self    self. .draw_circle draw_circle( (0 0, ,0 0, , height height, , top_radius top_radius, , slices slices, ,True True) )

python def draw_horn(self):

Main arm of the horn

glPushMatrix() glScalef(0.8, 0.2, 0.1) self.draw_cube(1.0, 1.0, 1.0) glPopMatrix()

Cross arm of the horn

glPushMatrix() glScalef(0.2, 0.8, 0.1) self.draw_cube(1.0, 1.0, 1.0) glPopMatrix() 4. Technical Considerations 4.1 Performance Optimization • OpenGL display lists for efficient rendering • Threading model separates I/O from graphics • Frame rate limiting to 60 FPS to balance responsiveness and resource usage 4.2 Cross-Platform Compatibility • Compatible with Windows, macOS, and Linux systems • Fallback rendering mechanisms if GLUT is unavailable • Platform-specific port detection routines 4.3 Safety Features • Bounds checking for servo angles (constrained to 0-180°) • Timeout handling for serial operations • Graceful shutdown with complete resource cleanup 5. Future Enhancements The digital twin system could be extended with: • Multiple servo support: Visualization of complex servo assemblies • Physical property simulation: Modeling torque, load, and power consumption • Data logging: Recording and analyzing servo performance over time • Network communication: Remote monitoring and control capabilities • Predictive maintenance: AI-based analysis of anomalous behavior 6. Conclusion This digital twin implementation provides a sophisticated virtual representation of a servo motor with:

  1. Real-time bidirectional communication with physical hardware
  2. High-quality 3D visualization with realistic materials and lighting
  3. Intuitive user controls via multiple input methods
  4. Robust error handling and recovery mechanisms The system demonstrates effective integration of graphics, hardware communication, and user interface technologies to create a functional digital twin for educational and development purposes. Technical Appendix Dependencies • Python 3.x • PyGame • PyOpenGL • PySerial Command Line Arguments--port PORT --baud BAUD --list-ports --sim
    Arduino serial port (e.g., COM3, /dev/ttyUSB0) Baud rate (default: 9600) List available serial ports and exit Run in simulation mode (no Arduino) Communication Protocol Specification Direction To Arduino Format Example S:[angle]\n From Arduino Description S:90\n Set servo angle command A:[angle] A:90 Angle feedback from servo

About

Digital twin using python, servo moter and arduno.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors