Free-piston engine (FPE), as an alternative of the conventional internal combustion engine, has a high potential in terms of energy saving given its wide range of potential applications and high efficiency. One of the technical barriers that prevents the wide spread of this technology is the lack of precise piston motion control. This paper presents a novel strategy on the motion control of a hydraulic FPE. A robust repetitive controller is designed and implemented to form a virtual crankshaft that would provide a precise and stable engine operation. Based on the controller, a hardware-in-the-loop (HIL) environment that integrates the combustion chamber dynamics with the hydraulic system is developed to further validate the proposed control design. The experimental data of engine motoring tests with virtual crankshaft control demonstrates the effectiveness of the proposed approach.